How do I make a multidimensional random walk?

7 visualizaciones (últimos 30 días)
Charlene Berns
Charlene Berns el 22 de Abr. de 2021
Respondida: Pratyush Roy el 10 de Mayo de 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

Respuesta aceptada

Pratyush Roy
Pratyush Roy el 10 de Mayo de 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!

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by