how can I find the max value out of iterative for loop ?

조회 수: 20 (최근 30일)
omar th
omar th 2022년 8월 1일
댓글: Matt J 2022년 8월 1일
here, the problem is when, for loop iterate itself let's say 5 times, the first maxvalue from is 9, the second is 3, the third is 5, the fourth is 7 and the fifth is 3, so the max_maxvalue (global max) must be 9 out of these five iterations. But I got 3 as max_maxvalue (global max) which means its chose the max value from the last iteration NOT saving the previous max values and compare among them according to what I want.
Thanks in advance for any help
count=1;
while 1
for i=1:20
x(i)= % calculations
end
maxvalue(count) = max(x);
count=count+1;
if x(end) == maxvalue(end)
break
end
end
max_maxvalue=max(maxvalue);

채택된 답변

Matt J
Matt J 2022년 8월 1일
Seems to be working:
count=1;
while 1
for i=1:20
x(i)= randi(87);% calculations
end
maxvalue(count) = max(x);
count=count+1;
if x(end) == maxvalue(end)
break
end
end
maxvalue
maxvalue = 1×3
86 86 77
max_maxvalue=max(maxvalue)
max_maxvalue = 86
  댓글 수: 2
omar th
omar th 2022년 8월 1일
first thank you for your response, but in first round from 1: 20, we can get the max for example 86, but when the for loop iterate again from 1: 20 we get the max value 77, now If I want to stop the code, so here the code consider 77 is the max value NOT consider 86 as the maximum value this is what I want to obtain it which is for example should consider 86 as max value
Matt J
Matt J 2022년 8월 1일
But as you can see in my RUN above, we did get a max_maxvalue of 86, as you desired.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by