How do I make a multidimensional random walk?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Pratyush Roy
2021-5-10
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 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!