set values after certain value is exceeded to 0

1 回表示 (過去 30 日間)
Frederik Reese
Frederik Reese 2022 年 6 月 27 日
回答済み: Karim 2022 年 6 月 27 日
Hi,
I have a matrix and want to set only values < 0.05 to 0, after in that specific row a value of 0.1 is exceeded. If the value 0.1 is not exceeded the row should remain unchanged.
Thanks
f.e. such a row
0 0 0.001 0.02 0.05 0.2 0.15 0.13 0.08 0.06 0.03 0.02 0.01 0 0 0 0

採用された回答

Karim
Karim 2022 年 6 月 27 日
One approach is to first look for the treshold value, build a logical array using that values and then use it together with an additional logic check to adjust the values
MyArray = [0 0 0.001 0.02 0.05 0.2 0.15 0.13 0.08 0.06 0.03 0.02 0.01 0 0 0 0];
% find first occasion where we go over 0.1
CheckMe = false(size(MyArray));
CheckMe( find(MyArray > 0.1, 1) : end) = true;
% afterwards, set all values smaller then 0.05 to 0
MyArray( CheckMe & MyArray < 0.05) = 0
MyArray = 1×17
0 0 0.0010 0.0200 0.0500 0.2000 0.1500 0.1300 0.0800 0.0600 0 0 0 0 0 0 0
% workarround to display the last values
MyArray(end-7 : end)
ans = 1×8
0.0600 0 0 0 0 0 0 0

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by