How to check if all the values in the array are greater or equal or lesser than a particular value?
147 次查看(过去 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 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!