Simulation of random walk
9 次查看(过去 30 天)
显示 更早的评论
I am simulating a random walk using markov chains . In the case below a 3x3 grid (9 nodes) is being used. I want to make 100 moves and store the postiion (node number) into an array. When I run the code below I get a list of the 100 postions reached but only the last node reached is stored. I want to find the number of times certain nodes are landed on within the 100 iterations and so need to store the 'j' values into an array but I am not sure how to do this. Any help would be greatfully recieved.
T = create_transition(3); % for example, a 3x3
k = 1; % start at node 1 (you have free choice)
trans_pdf = T(k,:); % get the PDF for node i
j = next_node(trans_pdf); % j now holds the destination node for this step in the simulation
for iter = 1:100
trans_pdf = T(j,:); % get the PDF for node i
j = next_node(trans_pdf); % j now holds the destination node for this step in the simulation
node_visited(i)=j
end
Node_1_visits = sum(j == 1);%Top left
Node_5_visits = sum(j== 5);% Middle
Node_6_visits = sum(j== 6);%Middle Right
function dest = next_node(trans_pdf)
cdf = cumsum(trans_pdf);
p = rand(); % uniform random
bool_test = p<=cdf;
[~, dest] = max(bool_test); % find the location of the first True
end
0 个评论
采纳的回答
Jon
2022-1-14
编辑:Jon
2022-1-14
It looks like you already "store the j values into the array node_visited.
So it looks like you should be doing your summaries, e.g.
Node_6_visits = sum(node_visited==6)
Also, if you post more code, it is good to use the "code" button on the MATLAB answers toolbar, this will format it as it appears in the MATLAB editor, making it more readable, and also easier for others to copy if they want to try running it.
2 个评论
Jon
2022-1-14
You assign node_visited using
node_visited(i)=j
But it looks like the index i does not change in your loop (actually I don't even see where it is assigned in the fragment of code you supplied). Instead you should assign to an index that is incremented by the for statement so
node_visited(iter)=j
更多回答(1 个)
Image Analyst
2022-4-22
For what it's worth (to others), I'm attaching all my random walk demos.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Markov Chain Models 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!