How do I make a multidimensional random walk?

5 views (last 30 days)
So, I have a 2D random walk, but how do I change my code so that it can calculate N dimensions? I will be asking for user input as to the number of dimensions.
nSteps = input('Enter the number of steps in a single run: ') % Length of the x-axis and random walk.
nRepeats = input('Enter the number of simulation runs to do: '); % The number of random walks.
w_position = (1) = 0;
for i=1:nRepeats
for j = 1:nSteps % Looping all values of nSteps into w_postion.
x = sign(randn); % Generates either +1/-1 depending on the sign of RAND.
w_position(j+1) = w_position(j) + x;
end
plot(w_position);
hold on
end

Accepted Answer

Pratyush Roy
Pratyush Roy on 10 May 2021
Hi,
For random walk in higher dimensions we can use a similar approach as mentioned in the code for 2 dimensional random walk. The code snippet below might be helpful to generate random walk in high dimensions:
nSteps = input('Enter the number of steps in a single run: ') % Length of the x-axis and random walk.
nRepeats = input('Enter the number of simulation runs to do: '); % The number of random walks.
nDims = input('Enter the number of Dimensions: '); % Data Dimensionality.
w_position = zeros(nSteps,nDims);
for i=1:nRepeats
for j = 1:nSteps % Looping all values of nSteps into w_postion.
x = sign(randn([1,nDims])); % Generates either +1/-1 depending on the sign of RAND.
w_position(j+1,:) = w_position(j,:) + x;
end
end
Here w_position stores the position at the ith instant in the ith row.
Hope this helps!

More Answers (0)

Community Treasure Hunt

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

Start Hunting!