Indexing into timetables with an array of logicals

14 vues (au cours des 30 derniers jours)
Rob
Rob le 6 Avr 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;

Réponses (2)

madhan ravi
madhan ravi le 6 Avr 2020
Modifié(e) : madhan ravi le 6 Avr 2020
It’s the simplest way you can do it.

Ameer Hamza
Ameer Hamza le 6 Avr 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)

Catégories

En savoir plus sur Timetables dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by