Self avoiding random walk help
Mostrar comentarios más antiguos
Dear all,
I am trying to create SAW random walk in matlab. First I have written a code which it can have 6possible steps on a lattice(code is attached).
Now I want to create a self avoiding random walk, which after the first step, it can only have 5 possible steps (cannot go back on itself). Any idea that how may I change the code? should I write another code?
clear all
clc
a = 1 ; % Step size
N = 2 ; % Number of Random Walks
S = 100 ; % Number of steps
r=zeros(1,1);
for k = 1:N
for i = 1:S
t = randi(6);
% Every step in "x" and "y" and "z" directions:
if t == 1
x{i} = 1;
y{i} = 0;
z{i} = 0;
elseif t == 2
x{i} = -1;
y{i} = 0;
z{i} = 0;
elseif t == 3
x{i} = 0;
y{i} = 1;
z{i} = 0;
elseif t == 4
x{i} = 0;
y{i} = -1;
z{i} = 0;
elseif t == 5
x{i} = 0;
y{i} = 0;
z{i} = 1;
elseif t == 6
x{i} = 0;
y{i} = 0;
z{i} = -1;
end
X = cell2mat(x);
Y = cell2mat(y);
Z = cell2mat(z);
end
X1=[r,X];
Y1=[r,Y];
Z1=[r,Z];
% We sum the steps to get all the data in a new cell array:
X2{k}=cumsum(X1);
Y2{k}=cumsum(Y1);
Z2{k}=cumsum(Z1);
end
% Now we have the data for N number individual random seeds, and
% we have it for S number of steps:
x2=cell2mat(X2);
y2=cell2mat(Y2);
z2=cell2mat(Z2);
% we get N sets of data for S random steps:
x_final=reshape(x2,[S+1,N]);
y_final=reshape(y2,[S+1,N]);
z_final=reshape(z2,[S+1,N]);
% We plot the Random walk:
plot3(x_final,y_final,z_final,'x-')
grid on
axis equal
Best,
Argu
4 comentarios
James Tursa
el 11 de Feb. de 2019
Cannot go back on itself only for the most recent step, or for any previous steps?
Adam Danz
el 11 de Feb. de 2019
Editada: Image Analyst
el 12 de Feb. de 2020
Here is a much cleaner, more efficient version of your code. The nested conditional statements were replaced with the randWalkmat matrix. If you have any questions please let me know.
I haven't addressed your question here. I just cleaned up the code to make it more efficient.
% Random walk with integer steps.
clear all
clc
a = 1 ; % Step size
N = 2 ; % Number of Random Walks
S = 100 ; % Number of steps
randWalkMat = [...
1 -1 0 0 0 0;
0 0 1 -1 0 0;
0 0 0 0 1 -1;
];
X = zeros(S,N);
Y = X;
Z = X;
for k = 1:N
t = randi(6, 1, S);
randWalk = randWalkMat(:, t);
X(:,k) = randWalk(1,:);
Y(:,k) = randWalk(2,:);
Z(:,k) = randWalk(3,:);
end
% Now we have the data for N number individual random seeds, and
% we have it for S number of steps:
% we get N sets of data for S random steps:
x_final = [[0,0];cumsum(X)];
y_final = [[0,0];cumsum(Y)];
z_final = [[0,0];cumsum(Z)];
% We plot the Random walk:
plot3(x_final,y_final,z_final,'x-')
grid on
axis equal

DEBIDATTA BEHERA
el 12 de Feb. de 2020
What will be the scilab code for self avoiding random walk?
Image Analyst
el 12 de Feb. de 2020
I'd copy the code to a scilab discussion forum and ask for help translating it into that language.
Respuesta aceptada
Más respuestas (1)
Categorías
Más información sobre Image Arithmetic en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!