find max value with for loop

69 次查看(过去 30 天)
sarp karayegen
sarp karayegen 2020-4-19
B=[ 6 7 8 9 10 ]
C=[8]
I want to have a for loop to compare each of the elements in B with the latest maximum number from the for loop to get the final maximum number in B.

回答(1 个)

Vimal Rathod
Vimal Rathod 2020-4-23
Hi,
You could use the following code snippet if you want to use for loops specifically to find max.
max = B(1);
for i = 2:numel(B)
if B(i) > max
max = B(i)
end
end
If you want to change something you could use this for loop but if your use case is to find max then I suggest using max function. Below is the link for max function.

类别

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