How do I make a multidimensional random walk?
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Charlene Berns
am 22 Apr. 2021
Beantwortet: Pratyush Roy
am 10 Mai 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 Kommentare
Akzeptierte Antwort
Pratyush Roy
am 10 Mai 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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!