Substract only higher then a limit? Please find the example program below.

Suppose
dataI = [100;200;300;100;200;300];
limit = 150;
if any(dataI >= limit)
disp('There is at least one value above the limit.')
DataI_with_Neg = dataI - 100; %% Here I want to sub 100 from all the values which are higher than limit = 150 and get new data after sub%%
else
disp('All values are below the limit.')
end

回答 (1 件)

madhan ravi
madhan ravi 2019 年 2 月 12 日
編集済み: madhan ravi 2019 年 2 月 12 日
DataI_with_Neg = dataI(dataI > limit) - 100;
% ^^^^^^^^^^^^^^^--- add this (datas which are higher than 150)

6 件のコメント

Rakesh Yadav Kodari
Rakesh Yadav Kodari 2019 年 2 月 12 日
Thank you for response, The problem is if i use the code as above it only return the values which are substracted.
I need to get the whole data along with the sub data.
madhan ravi
madhan ravi 2019 年 2 月 12 日
Data_higher_than_limit = dataI(dataI > limit)
Data_higher_than_limit_subtracted_by_100 = dataI(dataI > limit) - 100
John D'Errico
John D'Errico 2019 年 2 月 12 日
loc = dataI >= 150;
dataI(loc) = dataI(loc) - 100;
Stephen23
Stephen23 2019 年 2 月 12 日
Perhaps this?:
>> dataI = [100;200;300;100;200;300]
dataI =
100
200
300
100
200
300
>> limit = 100;
>> idx = dataI >= limit;
>> dataI(idx) = dataI(idx)-100
dataI =
0
100
200
0
100
200
madhan ravi
madhan ravi 2019 年 2 月 12 日
編集済み: madhan ravi 2019 年 2 月 12 日
Ah thank you John D'Errico & Stephen Cobeldick now it's clear what the OP wants.
Rakesh Yadav Kodari
Rakesh Yadav Kodari 2019 年 2 月 12 日
Thank you all.
Code from John D'Errico worked.
loc = dataI >= 150;
dataI(loc) = dataI(loc) - 100;

この質問は閉じられています。

質問済み:

2019 年 2 月 12 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by