find max value with for loop

조회 수: 64 (최근 30일)
sarp karayegen
sarp karayegen 2020년 4월 19일
답변: Vimal Rathod 2020년 4월 23일
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
David Hill
David Hill 2020년 4월 19일
What have you done? Is this homework? If not, why not just:
C=max(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