How do I make a multidimensional random walk?
9 views (last 30 days)
Show older comments
Charlene Berns
on 22 Apr 2021
Answered: Pratyush Roy
on 10 May 2021
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
0 Comments
Accepted Answer
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!
0 Comments
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!