# I have a for loop that makes one particle take 1000 random steps. How do i make this happen for 1000 particles?

1 visualización (últimos 30 días)
Nathan Clark el 1 de Mzo. de 2015
Comentada: ane4kina el 5 de Abr. de 2019
I am working on a project where I need to make 1000 particles, starting at the origin, take 1000 random steps and plot said steps. I got the first part which is:
N=1000;
position = zeros(2,N+1);
for i = 2:N+1
step=2*rand(2,1)-1; %Creates a random step vector (x,y)
position(:,i)= position(:,i-1)+step; %adds step to last position
end;
plot(position(1,2:N+1),position(2,2:N+1)) %Plots steps of particles starting from origin
This all works but now i need to make it work for 1000 particles and i need to plot them all on top of each other on the same graph. Does anyone have any suggestions? Thanks for the help.
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Respuestas (1)

Andrew Newell el 1 de Mzo. de 2015
Editada: Andrew Newell el 1 de Mzo. de 2015
This is a 2D random walk, and it helps to use variable names that reflect this. It is more efficient to generate a lot of random numbers at once and then use cumsum to cumulatively add them:
nSteps = 1000;
nParticles = 1000;
x = cumsum(2*rand(nSteps,nParticles)-1);
y = cumsum(2*rand(nSteps,nParticles)-1);
plot(x,y)
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
ane4kina el 5 de Abr. de 2019
What is you wanted them to start at the origin and only plot the final position of each particle?

Iniciar sesión para comentar.

### Categorías

Más información sobre 2-D and 3-D Plots 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!

Translated by