Double for loop in a random walk. How do I loop over all particles simultaneously instead of looping the amount of steps for each separate particle one at a time?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Delshad Ayoubi
el 22 de Feb. de 2018
Comentada: Guillaume
el 23 de Feb. de 2018
I currently have a script that utilizes a double for loop in order to create a random walk in 2D. It however is looping over all the steps of the random walk one particle at a time. That is, it loops over N steps for one particle, then it moves on to the M:th particle and loops it over all N. I want to loop all particles M simultaneously instead of one at a time. I don't know how to do this, tried implementing a zero vector and inserting all values of x_t and y_t in it and plotting it but it didn't work, the figure looked weird. Here is the code:
clear
clc
N = 300; % Length of the x-axis, also known as the length of the random walks.
M = 40; % The amount of random walks.
x_t(1) = 0;
y_t(1) = 0;
radie=50;
x=0;
y=0;
theta=linspace(0,2*pi);
for m=1:M
for n = 1:N % Looping all values of N into x_t(n).
A = sign(randn); % Generates either +1/-1 depending on the SIGN of RAND.
x_t(n+1) = x_t(n) + A;
A = sign(randn);
y_t(n+1) = y_t(n) + A;
distance = sqrt(x_t(n+1)^2 + y_t(n+1)^2);
while distance > radie % så att de ej passerar cirkeln.
A = sign(randn); % Generates either +1/-1 depending on the SIGN of RAND.
x_t(n+1) = x_t(n) + A;
A = sign(randn);
y_t(n+1) = y_t(n) + A;
% Compute distance again with new values.
distance = sqrt(x_t(n+1)^2 + y_t(n+1)^2);
end
end
plot(x_t, y_t,'Markersize',10)
hold on
plot(x+radie*cos(theta),y+radie*sin(theta));
axis equal
end
grid on
I want all of the M particles to start moving N steps at the same time instead of looping all of N over each of the inputs M contains.
2 comentarios
Guillaume
el 22 de Feb. de 2018
I don't fully understand your question, is it related to the plotting or to the calculation of x_t or y_t? What should be different from what you have already?
Unrelated to your question:
A = sign(randn); % Generates either +1/-1 depending on the SIGN of RAND.
Technically, this can also generate 0 although very unlikely. A guaranteed way to just generate +-1:
A = (-1).^randi(2);
Also,
somevalue = somelongcalculation
while somevalue > somethreshold
somevalue = somelongcalculationlc;
end
is more elegantly written as:
somevalue = inf;
while somevalue > somethreshold
somevalue = somelongcalculationlc;
end
Respuesta aceptada
Guillaume
el 22 de Feb. de 2018
If you want to change the order of the loops, then your x_t and y_t need to be 2d matrices
x_t = zeros(M, N+1);
y_t = zeros(M, N+1);
for n = 1:N
for m = 1:M
distance = inf;
while distance > radie
x_t(m, n+1) = x_t(m, n) + (-1).^randi(2);
y_t(m, n+1) = y_t(m, n) + (-1).^randi(2);
distance = hypot(x_t(m, n+1), y_t(m, n+1));
end
end
end
plot(x_t, y_t, 'Markersize', 10);
6 comentarios
Guillaume
el 23 de Feb. de 2018
It's not for some reason, it's because you're replotting all the points at each step of the loop, including the ones you've plotted on the previous steps. Instead of plotting MxN = 1200 points, you're plotting MxNx(N+1)/2 = 2257500 points, so yes it's going to be slow.
You either need to move all the plotting out of the loops, as I'd written or if you want it in the loop so you can see the progress:
for n = 1:N
for m = 1:M
distance = inf;
while distance > radie % så att de ej passerar cirkeln.
x_t(m, n+1) = x_t(m, n) + (-1).^randi(2);
y_t(m, n+1) = y_t(m, n) + (-1).^randi(2);
distance = hypot(x_t(m, n+1), y_t(m, n+1));
end
end
plot(x_t(:, 1:n+1)', y_t(:, 1:n+1).','.', 'MarkerSize', 5);
hold on
plot(x+radie*cos(theta),y+radie*sin(theta));
axis equal
hold off
drawnow
end
The hold off and drawnow I've added are both critical. It's still going to be slower than doing the plotting after the loops because at each step you're erasing the previous plot and replotting everything.
Más respuestas (1)
James Tursa
el 22 de Feb. de 2018
If you want to walk all of the particles simultaneously, then simply reverse the order of your for-loops:
for n = 1:N % Looping all values of N into x_t(n).
for m=1:M
You will also need to modify all of the interior loop code to account for M particles. Currently the x_t and y_t variables are only dimensioned for 1 particle. So maybe turn them into arrays so you are using x_t(n,m) and y_t(n,m) etc within the loop.
Also this test doesn't look correct:
while distance > radie % så att de ej passerar cirkeln.
It will always fail at the start of your run. Maybe you meant to use < instead?
1 comentario
Ver también
Categorías
Más información sobre Matrix Indexing 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!