can I compare the previous state with current state in " While loop " ?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
0 comentarios
Respuesta aceptada
Voss
el 9 de Jun. de 2022
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
2 comentarios
Voss
el 10 de Jun. de 2022
% ...
% 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
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!