Indexing into timetables with an array of logicals

14 visualizaciones (últimos 30 días)
Rob
Rob el 6 de Abr. de 2020
Respondida: Ameer Hamza el 6 de Abr. de 2020
Hi,
Is it possible to index into a timetable (or table) with an array of logicals for the purpose of assigning, say, NaNs wherever an element of the array is TRUE?
Here's a simple example:
% Create a timetable:
dates = datetime([2010 1 1;
2011 1 1;
2012 1 1;
2013 1 1;
2014 1 1]);
data = array2timetable(rand(5, 3), 'RowTimes', dates);
% Find where the values are less than 0.5:
idx = data{:, :} < 0.5;
% How do I directly replace the values in 'data' indexed by 'idx' with NaNs?
% For example, I thought this might work, but it doesn't: data{idx} = NaN;
% It appears I'm unable to do so without doing something a bit convoluted like this:
tmp = data{:, :};
tmp(idx) = NaN;
data{:, :} = tmp;

Respuestas (2)

madhan ravi
madhan ravi el 6 de Abr. de 2020
Editada: madhan ravi el 6 de Abr. de 2020
It’s the simplest way you can do it.

Ameer Hamza
Ameer Hamza el 6 de Abr. de 2020
The easiest way might be to convert the timetable to an array, convert values to nan based on the condition and recreate the timetable. However, the following show an example of how to do it directly
% Create a timetable:
dates = datetime([2010 1 1;
2011 1 1;
2012 1 1;
2013 1 1;
2014 1 1]);
data = array2timetable(rand(5, 3), 'RowTimes', dates);
% Find where the values are less than 0.5:
idx = data{:, :} < 0.5;
x = [0 nan];
data.Variables = data.Variables.*~idx + x(idx+1)

Categorías

Más información sobre Timetables en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by