How to check if all the values in the array are greater or equal or lesser than a particular value?

230 ビュー (過去 30 日間)
In the first IF loop, i want to check if any of the values in SPEED(SPEED is an array holding 72000 values) are greater than max(num1), if so then visit the second IF loop just for that particular value of SPEED array not for for all the values. And in the second IF loop, i want to check if any of the values of TCADJUSTEDPERCENTLOAD array(TCADJUSTEDPERCENTLOAD is an array holding 72000 values) is greater than interp1(finalized,final, SPEED) if so then AdjSpeed=interp1(final,finalized,TCADJUSTEDPERCENTLOAD) for that particular value of TCADJUSTEDPERCENTLOAD array. Else of first IF is AdjSpeed = SPEED.
i wrote the below code but it is just checking with the first value of SPEED and first value of TCADJUSTEDPERCENTLOAD not with rest 71999 values, any help is really apreciated, Thank you!!
if (SPEED > max(num1))
if (TCADJUSTEDPERCENTLOAD > interp1(finalized,final, SPEED))
AdjSpeed=interp1(final,finalized,TCADJUSTEDPERCENTLOAD)
end
else
AdjSpeed = SPEED
end
  2 件のコメント
madhan ravi
madhan ravi 2019 年 1 月 7 日
select the code and press the code button so that it's easy to read
raghavendra kandukuri
raghavendra kandukuri 2019 年 1 月 7 日
編集済み: raghavendra kandukuri 2019 年 1 月 7 日
appologies for that, Done

サインインしてコメントする。

採用された回答

Jan
Jan 2019 年 1 月 7 日
It seems like you are confused by the term "if loop". The if command is not a loop.
if needs a scalar condition. If SPEED is a vector, this is evaluated implicitly:
if all(SPEED > max(num1))
You want to check all elements of SPEED, so you need a loop:
for k = 1:numel(SPEED)
if SPEED(k) > max(num1)
...
end
end
You need anotehr loop for TCADJUSTEDPERCENTLOAD also (by the way, this word is not readable - use a leaner name instead).
Do not evaluated e.g. max(num1) or interp1(finalized,final, SPEED) inside the loop, but before, to save processing time.
For more efficient code, logical indexing will help you:
match = (SPEED > max(num1));
Now e.g. SPEED(match) contains only elements of speed beyond the limit.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by