how can we use max function inside while loop ?

6 次查看(过去 30 天)
My question, can we choose the maximimum value out of the maxvalue as explained below the first maxvalue 4 the second maxvalue 8, the third maxvalue 3 the fourth maxvalue 2 and the fifth max value is 6. ??
thanks in advance
while true
for i=1:20
x= do calculation from 1 up to 20 % (1:20= we call it one round)
end
[maxvalue(i), indxmax] = max(x(:));
maxvalue will be one value in each for loop round, but for loop will be repeated let say 5 times
so, maxvalue will be 4 in first 20 step, 8 in the second 20, 3 in third round, and so forth
up to 5 round
if x(i) >= maxvalue(i) % This condition to break the while loop
break
end
end

采纳的回答

David Hill
David Hill 2022-7-29
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);
  2 个评论
omar th
omar th 2022-8-1
David Hill , please would you tell how can I find the global max. For example, if we for loop iterate its self 5 times, so, maxvalue from these iteration is 8, 3, 4, 5,6, here the max_maxvalue consider only the last maxvalue as max_maxvalue not compare among the max value og the 5 iterations and consider 8 as the max_maxvalue, I dont know to make that

请先登录,再进行评论。

更多回答(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