Random Walk boundaries not working

The particles end up outside the barrier
M = 100; % the number of particles
N = 80; % the number of jumps to take
Deltax = 1; % the size of the jumps in x
Deltay = 1; % the size of the jumps in y
[row,col] = find(mask); %'mask' is the barrier in which 0 are the barrier and 1 is where particles are free to roam
Position=randi(length(row),M,1);
x=col(Position); % set x position for the particle
y=row(Position); % set y position for the particle
hold on
plot(x,y, '.', 'MarkerSize', 20)
for n = 1:N % for each of the N jumps
r = rand(1,M); % generate M random numbers between 0 and 1
left_mask = find(r < 0.25); % mask identifying the left-moving particles
for idx = 1:length(left_mask)
xLoc = x(left_mask);
yLoc = y(left_mask);
if mask(xLoc - Deltax, yLoc) == 0
x(left_mask(idx)) = x(left_mask(idx)) - Deltax;
end
end
% move those particles left
right_mask = find((0.25 <= r) & (r < 0.5));
for idx = 1:length(right_mask)
xLoc = x(right_mask);
yLoc = y(right_mask);
if mask(xLoc + Deltax, yLoc) == 0
x(right_mask(idx)) = x(right_mask(idx)) + Deltax;
end
end
down_mask = find((0.5<= r) & (0.75>r)); % mask identifying the down-moving particles
for idx = 1:length(down_mask)
xLoc = x(down_mask);
yLoc = y(down_mask);
if mask(xLoc - Deltay, yLoc) == 0
x(down_mask(idx)) = x(down_mask(idx)) - Deltay;
end
end
% move those particles down
up_mask = find(r >= 0.75); % mask identifying the up-moving particles
for idx = 1:length(up_mask)
xLoc = x(up_mask);
yLoc = y(up_mask);
if mask(xLoc + Deltay, yLoc) == 0
x( up_mask(idx)) = x( up_mask(idx)) + Deltay;
end
end

Respuestas (0)

Categorías

Más información sobre Sparse Matrices en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 24 de Mayo de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by