can I compare the previous state with current state in " While loop " ?

2 次查看(过去 30 天)
While loop used to move the ponts from position to position in one shot (each one second). now my question can I can iterate the while loop and
compare the past step with currenct step, for example If we want to calculate the X inside the while loop, we should compare X(i) with X(i-1) ?
Thanks in advance
npts=1;center=[0 0];radius=1000;
npts2=1;center2=[0 0];radius2=1000;
% Initial direction/velocity of the points
velocity = 28.8/3.6;
velocity2 = 28.8/3.6;
theta = rand(npts, 1) * 2*pi;
g = 0.5 * radius + 0.5 * radius * rand(npts,1);
X_x=center(1)+g.*cos(theta);
Y_y=center(2)+g.*sin(theta);
XY = [X_x ,Y_y];
theta2 = rand(npts2, 1) * 2*pi;
g2 = 0.5 * radius2 + 0.5 * radius2 * rand(npts2,1);
X_x2=center2(1)+g2.*cos(theta2);
Y_y2=center2(2)+g.*sin(theta2);
XY2 = [X_x2 ,Y_y2];
hfig = figure('Color', 'w');
hax = axes('Parent', hfig);
hdots(1) = plot(XY(1,1),XY(1,2),'Parent', hax,'Marker', '.','Color', 'k','LineStyle', 'none','MarkerSize', 10);
hold(hax, 'on')
axis(hax, 'equal')
hdots(2) = plot(XY2(1,1),XY2(1,2),'Parent', hax,'Marker', '.','Color', 'r','LineStyle', 'none','MarkerSize', 10);
hold(hax, 'on')
axis(hax, 'equal')
% Plot the circl e as a reference
t = linspace(0, 2*pi, 100);
plot(radius * cos(t) + center(1),radius * sin(t) + center(2))
while all(ishghandle(hdots)) %Timestep=1:1:Totaltime
direction2 = rand(npts, 1) * 2 *pi;
direction = rand(npts, 1) * 2 *pi;
[XY2, direction2] = step(XY2, direction2, velocity2, radius2, center2);
% Plot the dots as black markers
[XY, direction] = step(XY, direction, velocity, radius, center);
% Update the dot plot to reflect n ew locations
set(hdots(2), 'XData', XY2(1,1), 'YData', XY2(1,2))
set(hdots(1), 'XData', XY(1,1), 'YData', XY(1,2))
% Force a r edraw
drawnow
%pause (1)
end

采纳的回答

Voss
Voss 2022-6-9
X_current = 1;
X_previous = 0;
while true
fprintf('current = %4.2f, previous = %4.2f\n',X_current,X_previous);
if abs(X_current-X_previous) < 0.1
fprintf('close enough\n');
break
end
X_previous = X_current;
X_current = X_previous + rand();
end
current = 1.00, previous = 0.00 current = 1.23, previous = 1.00 current = 1.71, previous = 1.23 current = 2.52, previous = 1.71 current = 3.25, previous = 2.52 current = 3.94, previous = 3.25 current = 4.33, previous = 3.94 current = 5.13, previous = 4.33 current = 5.65, previous = 5.13 current = 5.79, previous = 5.65 current = 5.80, previous = 5.79
close enough
  2 个评论
omar th
omar th 2022-6-10
First, thank you so much for your reponse. Regards to my code " while all(ishghandle) " used to move the random points each one second as one shot. Now, can I compare sigdB (line 37) for current step with sigdB for the previous step ?
Thanks in advance.
npts=1;center=[0 0];radius=1000;
npts2=1;center2=[0 0];radius2=1000;
% Initial direction/velocity of the points
velocity = 45/3.6;
velocity2 = 45/3.6;
theta = rand(npts, 1) * 2*pi;
g = 0.5 * radius + 0.5 * radius * rand(npts,1);
X_x=center(1)+g.*cos(theta);
Y_y=center(2)+g.*sin(theta);
XY = [X_x ,Y_y];
theta2 = rand(npts2, 1) * 2*pi;
g2 = 0.5 * radius2 + 0.5 * radius2 * rand(npts2,1);
X_x2=center2(1)+g2.*cos(theta2);
Y_y2=center2(2)+g.*sin(theta2);
XY2 = [X_x2 ,Y_y2];
hfig = figure('Color', 'w');
hax = axes('Parent', hfig);
hdots(1) = plot(XY(1,1),XY(1,2),'Parent', hax,'Marker', '.','Color', 'k','LineStyle', 'none','MarkerSize', 10);
hold(hax, 'on')
axis(hax, 'equal')
hdots(2) = plot(XY2(1,1),XY2(1,2),'Parent', hax,'Marker', '.','Color', 'r','LineStyle', 'none','MarkerSize', 10);
hold(hax, 'on')
axis(hax, 'equal')
% Plot the circl e as a reference
t = linspace(0, 2*pi, 100);
plot(radius * cos(t) + center(1),radius * sin(t) + center(2))
while all(ishghandle(hdots)) %Timestep=1:1:Totaltime
distPoints = pdist2(XY,XY2,'euclidean')
sig=sum(power*(distPoints).^-2);
sigdB=10*log(sig)
direction2 = rand(npts, 1) * 2 *pi;
direction = rand(npts, 1) * 2 *pi;
[XY2, direction2] = step(XY2, direction2, velocity2, radius2, center2);
% Plot the dots as black markers
[XY, direction] = step(XY, direction, velocity, radius, center);
% Update the dot plot to reflect n ew locations
set(hdots(2), 'XData', XY2(1,1), 'YData', XY2(1,2))
set(hdots(1), 'XData', XY(1,1), 'YData', XY(1,2))
% Force a r edraw
drawnow
pause (1)
end
Voss
Voss 2022-6-10
% ...
% your code
% ...
sigdB = NaN; % choose an appropriate initial value for sigdB
while all(ishghandle(hdots))
sigdB_previous = sigdB;
distPoints = pdist2(XY,XY2,'euclidean')
sig=sum(power*(distPoints).^-2);
sigdB=10*log(sig)
if isequal(sigdB,sigdB_previous) % Choose an appropriate comparison
% and take appropriate action.
end % Maybe the comparison should be somewhere
% else in the loop - I don't know.
% ...
% your code
% ...
end

请先登录,再进行评论。

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