# How do you find the maximum number of row vectors with loop?

5 views (last 30 days)
Sean Paul on 23 Sep 2017
Answered: John Chilleri on 23 Sep 2017
I manage to get it partially correct,but what if the maximum number is not the latest loop number(m)? For example, if A=[5 11 10 8]
Clearly ,max=11 .However,the answer matlab given is 10 since 10>8.I tried making if statement but can't manage to solve it.
for m=1:M
if A(m)> A(n,end)
max=A(m)
end
end
##### 0 CommentsShow -1 older commentsHide -1 older comments

Sign in to comment.

### Accepted Answer

John Chilleri on 23 Sep 2017
Hello,
You can find the maximum number in a vector with a loop by keeping the largest value stored as you progress over all the values.
You could alternatively use the max function. Anyways, here's how with a loop:
A = [5 11 10 8];
Maxval = A(1);
for i = 2:length(A)
if (Maxval < A(i))
Maxval = A(i);
end
end
Hope this helps!
##### 0 CommentsShow -1 older commentsHide -1 older comments

Sign in to comment.

### 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!