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 allclca = 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:St = 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 onaxis equal
Best,
Argu
Best Answer