Simulation of random walk

101 ビュー (過去 30 日間)
Ben Hatrick
Ben Hatrick 2022 年 1 月 14 日
回答済み: Image Analyst 2022 年 4 月 22 日
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

採用された回答

Jon
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 件のコメント
Ben Hatrick
Ben Hatrick 2022 年 1 月 14 日
When I do this i still get Node_6_visits = 0 even though I can see it has been visited in the list. For some reason it is only saving the last node visited.
Jon
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
Image Analyst 2022 年 4 月 22 日
For what it's worth (to others), I'm attaching all my random walk demos.

カテゴリ

Help Center および File ExchangeMarkov Chain Models についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by