Making a script that will generate a random "walker" that will walk along the x-axis randomly.

6 次查看(过去 30 天)
Alright, so basically I need to make a "random walker" that walks in either direction using a for loop. It can walk forward one, back one back one forward one, it just has to be random for a thousand times. Then I need to use comet to simulate the walk, and im just not sure how to set up this code. So far I have this. I need to plot the walk as a function of time. I don't know what I'm doing wrong.
%%Part 1
% Simulation of Walker
t = linspace(0,1001,50000);
x = 0;
d = randn(0,2);
for n = 1:1001
if d == 0
x = x;
elseif d == 1
x = x + 1;
else d == 2
x = x - 1;
end
end
comet(x,t);
  1 个评论
John D'Errico
John D'Errico 2018-11-4
You described a random walk that moves EITHER FORWARD OR BACKWARDS. But your code seems to allow for staying in the same spot too at any step.
Which is it? You need to choose one.

请先登录,再进行评论。

采纳的回答

John D'Errico
John D'Errico 2018-11-4
I'll guess:
n = 1000;
x = cumsum([0,-1 + 2*(rand(1,n-1)>0.5)]);
comet(x,0:n-1)

更多回答(1 个)

Image Analyst
Image Analyst 2022-8-9

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by