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

237 views (last 30 days)
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

Accepted Answer

Jan
Jan on 7 Jan 2019
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.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!