How can I find the max value from many max values inside the " WHILE LOOP " ?

5 次查看(过去 30 天)
if we run the code below we will get the maxpower in each round of "for loop" means the for loop starts from -28.64 up to +28.64 which means
20 steps. Now, the maxpower is obtained from each round (20 steps of the for loop). My question is how can I find the maximum value from many maxpower values inside while loop ?
Thanks in advance
Angleoption11 = [-28.6479 -27.5 -18.33 -13.75 -11 -9.16 -7.85 -6.87 -6.11 -5.5 5.5 6.11 6.87 7.85 9.16 11 13.75 18.33 27.5 28.6479];
while true
previouspower = currentpower;
for current_index1 = 1:numel(Angleoption11)
pickangle11 = Angleoption11(current_index1);
distance = sqrt((x1-x2.').^2 + (y1-y2.').^2)
currentpower=pt*distance
end
[maxpower, index]=max(currentpower);
end
  4 个评论
dpb
dpb 2022-7-26
Save the max you find each iteration in another array -- when the outer iteration is complete, then use max on that array.
Or, you can compare the new maximum to the previous each time and save the greater on each pass -- to do this you also must have a GlobalMax value that starts of first iteration holding the maximum; for subsequent does the comparison first.
omar th
omar th 2022-7-26
" Or, you can compare the new maximum to the previous each time and save the greater on each pass -- to do this you also must have a GlobalMax value that starts of first iteration holding the maximum; for subsequent does the comparison first."
thank you so much for your reply, actually this is what I want GlobalMax, so would you explain it more ?

请先登录,再进行评论。

采纳的回答

David Hill
David Hill 2022-7-26
GlobalMax=-9999;
while true
for
%calculations
end
if maxpower>GlobalMax
GlobalMax=maxpower;
end
end
  3 个评论
David Hill
David Hill 2022-7-26
while true
for
%calculations
end
if maxpower>GlobalMax
GlobalMax=maxpower;
else
break;%break when the maxpower stops increasing?
end
end
omar th
omar th 2022-7-26
Let's say by using for loop 20 values will be calculated again for loop will produce 20 values and so on. Here I want to compare the the max value from the the first 20 value and max value from the second 20 value and so on. let say for loop will work for 5 times. Now I have 5 max values how can I choose one max value from these 5 max values and consider the selected max value as Global max

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by