Indexing into timetables with an array of logicals

2 ビュー (過去 30 日間)
Rob
Rob 2020 年 4 月 6 日
回答済み: Ameer Hamza 2020 年 4 月 6 日
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;

回答 (2 件)

madhan ravi
madhan ravi 2020 年 4 月 6 日
編集済み: madhan ravi 2020 年 4 月 6 日
It’s the simplest way you can do it.

Ameer Hamza
Ameer Hamza 2020 年 4 月 6 日
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)

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by