find max value with for loop
188 views (last 30 days)
Show older comments
sarp karayegen on 19 Apr 2020
Answered: Vimal Rathod on 23 Apr 2020
B=[ 6 7 8 9 10 ]
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.
Vimal Rathod on 23 Apr 2020
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)
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.
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!