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

조회 수: 236 (최근 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

채택된 답변

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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by