MATLAB: Self avoiding random walk help

MATLAB

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

Best Answer

Working from the cleaner version of your code I provided in the comments under your question, here's an adapted version that avoids random steps that go back on the previous step.
Here are som key parts of the code
  • 'randWalkMat' is a matrix of random steps; rows are X,Y,Z
  • 'forbddenPairs': each row defines two columns of randWalkMat that would cancel out a step.
  • The for-loop and the nested while-loop are designed to choose a random column in randWalkMat but to check that the chosen column is not a forbidden pair with the previously chosen column. If so, it chooses another one.
  • Everything after that is just a cleaner version of your original code.
Feel free to ask followup questions!
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;
];
% list columns of randWalkMat that shouldn't be neighbors
% * Each row needs to be in accending order
forbiddenPairs = [
1,2;
3,4;
5,6];
X = zeros(S,N);
Y = X;
Z = X;
t = randi(6);
for k = 1:N
% Choose random indices of randWalkMat
for i = 2:S
accepted = false; %reset flag
while ~accepted
t(i) = randi(6);
% Determine if current step goes back on previous step
if ~ismember(sort(t(i-1:i)), forbiddenPairs, 'rows')
accepted = true; %step is OK, move on.
end
end
end
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 = [zeros(1,N);cumsum(X)]; % *
y_final = [zeros(1,N);cumsum(Y)]; % *

z_final = [zeros(1,N);cumsum(Z)]; % *
% We plot the Random walk:
plot3(x_final,y_final,z_final,'x-')
grid on
axis equal
Related Question