Convert negative values to zero in timetable
11 ビュー (過去 30 日間)
古いコメントを表示
I have a timetable that has 4 columns that are Date, NOConc, NO2Conc, NOxConc. I want to convert any negative numbers in the NOConc column to zero. The S function pulls the data from the larger timetable, TN_1, for a specific time range that I'm looking at (in this case 5/11/2022 - 5/18/2022) and makes the new timetable labeled Holly_NOx, with a variable amount of rows depending on the given date range. This code is the closest I've gotten, but it gives me the error "A variable index must be a real positive integer." I've seen some people say matlab wont allow indexing of negative integers, which would make what I'm trying to do impossible. Thanks!!
S = timerange('5/11/2022' , '5/18/2022', 'days');
Holly_NOx = TN_1(S,:);
if Holly_NOx.(Holly_NOx.NO2Conc) > 0
Holly_NOx.(Holly_NOx.NO2Conc) = 0;
end
0 件のコメント
採用された回答
Voss
2022 年 6 月 9 日
編集済み: Voss
2022 年 6 月 9 日
% a timetable with 3 data columns:
Holly_NOx = timetable(datetime(now+(-29:-22),'ConvertFrom','datenum').', ...
randn(8,1),randn(8,1),randn(8,1), ...
'VariableNames',{'NOConc' 'NO2Conc' 'NOxConc'});
disp(Holly_NOx);
% replace negative values in NOConc column of Holly_NOx with 0:
Holly_NOx.NOConc(Holly_NOx.NOConc < 0) = 0;
disp(Holly_NOx);
% or replace negative values in *any* column of Holly_NOx with 0:
Holly_NOx{:,:}(Holly_NOx{:,:} < 0) = 0;
disp(Holly_NOx);
% another way to replace all negative values with 0:
Holly_NOx{:,:} = max(0,Holly_NOx{:,:});
disp(Holly_NOx);
7 件のコメント
Peter Perkins
2022 年 6 月 13 日
Voss gets points for using braces on a table to modify the values. For more context on that, see the Arithmetic on Table Variables section of this example from the doc.
Another way to do this would be
Holly_NOx = varfun(@(x)max(x,0),Holly_NOx);
For large timetables, that's likely to be faster.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!