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

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 Comments

It is unclear what this code does or is intended to do. E.g.,
  • There is no break out of the forever while loop, so the code will never terminate on its own
  • previouspower is never used
  • pickangle11 is never used
  • The distance calculation doesn't depend on anything in the loop
  • pt is not defined anywhere
  • x1, x2, y1, y2 are not defined anywhere
  • Etc.
It is impossible to advise you how to get your desired result when your code does not appear to be a minimal working example. Or maybe you could use some numeric example inputs and show us what your desired numeric outputs would be.
first thank you for your fast response. Second, actually I just wanted to give simple example to make it easy for understanding. Below the the code that I wanted to find the maxpower. if you run this code you see 20 value in for loop and max function obtain ONE value from this 20 values, again for loop will be excuted 20 values and again max function will obtain one value and so forth.... at the end of the code will be stopped for example when the code is break
there are 5 maxpower values. My question I want to find the max value from these 5 values. I hope to let you get my question
thanks in advance, again
function UAVBSsMobilityBasedOnSINR(NumDrone, v, ro, center)
Numpoint1 = 1;center1=[0 0];center2=[0 0];
Numpoint2 = 1;ro1=1000;ro2=1000; vs1=0;vs2=0;v1=15/3.6;v2=15/3.6;
theta_point1=2*pi*(rand(Numpoint1,1)); % distributed random number of random point
g1 = 0.5 * ro1 + 0.5 * ro1 * rand(Numpoint1,1); % let the point deployed away from the center of circle network layout
Pospoint1_x=center1(1)+g1.*cos(theta_point1); % Initial positions
Pospoint1_y=center1(2)+g1.*sin(theta_point1);
Pospoint1 = [Pospoint1_x ,Pospoint1_y];
theta_point2=2*pi*(rand(Numpoint2,1)); % distributed random number of random point
g2 = 0.5 * ro2 + 0.5 * ro2 * rand(Numpoint2,1); % let the random point deployed away from the center of circle network layout
Pospoint_x=center2(1)+g2.*cos(theta_point2); % Initial positions
Pospoint2_y=center2(2)+g2.*sin(theta_point2);
Pospoint2 = [Pospoint_x ,Pospoint2_y];
hfig = figure('Color', 'w');
hax = axes('Parent', hfig);
hpoint(1) = plot(Pospoint1(1,1),Pospoint1(1,2),'Parent', hax,'Marker', '*','Color', 'k','LineStyle', 'none','MarkerSize', 12);
hold(hax, 'on')
axis(hax, 'equal')
hpoint(2) = plot(Pospoint2(1,1),Pospoint2(1,2),'Parent', hax,'Marker', '*','Color', 'r','LineStyle', 'none','MarkerSize', 12);
hold(hax, 'on')
axis(hax, 'equal')
ro=1000;center=[0 0];
t1 = linspace(0, 2*pi, 100);
plot(ro * cos(t1) + center(1), ro * sin(t1) + center(2));
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];
Angleoption22 = [-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];
alpha =2;pt=10;
currentpower1=zeros(1,1);
currentpower2=zeros(1,1);
while true
previouspower1 = currentpower1;
previouspower2 = currentpower2;
for current_index1 = 1:numel(Angleoption11)
pickangle11 = Angleoption11(current_index1);
dist_d1_d2 = sqrt((Pospoint_x-Pospoint1_x.').^2 + (Pospoint2_y-Pospoint1_y.').^2)
previouspower1 = currentpower1;
currentpower1 = pt* (dist_d1_d2)^-alpha;
[Pospoint1, pickangle11] = moveit1(Pospoint1, pickangle11, v1, ro1, center1);
fprintf(' 1 Change its Strategy ')
set(hpoint(1), 'XData', Pospoint1(1,1), 'YData', Pospoint1(1,2))
drawnow
pause(1)
end
for current_index2 = 1:numel(Angleoption22)
pickangle22 = Angleoption22(current_index2);
dist_d1_d2 = sqrt((Pospoint_x-Pospoint1_x.').^2 + (Pospoint2_y-Pospoint1_y.').^2)
currentpower2 = pt* (dist_d1_d2)^-alpha
previouspower2 = currentpower2;
[Pospoint2, pickangle22] = moveit1(Pospoint2, pickangle22, v1, ro1, center1);
fprintf(' 2 Change its Strategy ')
set(hpoint(2), 'XData', Pospoint2(1,1), 'YData', Pospoint2(1,2))
drawnow
pause(1)
end
[maxpower1, ~] = max(currentpower1);
[maxpower2, ~] = max(currentpower2);
condition1 = currentpower1 > maxpower1;
if condition1
[Pospoint1,pickangle11] = stop1(Pospoint1,pickangle11, vs1,ro1, center1);
fprintf(' P 1 ')
end
condition2 = currentpower2 > maxpower2;
if condition2
[Pospoint1,pickangle22] = stop1(Pospoint1,pickangle22, vs1,ro1, center1);
fprintf(' P 2 ')
end
cond11 = ((currentpower1-previouspower1)<0.1) && ((currentpower1-previouspower2)<0.1);
condition = condition1 & condition2 & cond11;
if condition
break
end
end
end
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.
" 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 ?

Sign in to comment.

 Accepted Answer

GlobalMax=-9999;
while true
for
%calculations
end
if maxpower>GlobalMax
GlobalMax=maxpower;
end
end

3 Comments

first thank you for your response. If the global max generated during the excution which means during running the code at some point the global max power will be reached so I want the code stop at this point when reached that Global max
while true
for
%calculations
end
if maxpower>GlobalMax
GlobalMax=maxpower;
else
break;%break when the maxpower stops increasing?
end
end
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

Sign in to comment.

More Answers (0)

Products

Release

R2018a

Asked:

on 26 Jul 2022

Commented:

on 26 Jul 2022

Community Treasure Hunt

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

Start Hunting!