Simulation of random walk
Mostrar comentarios más antiguos
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
Respuesta aceptada
Más respuestas (1)
Image Analyst
el 22 de Abr. de 2022
0 votos
For what it's worth (to others), I'm attaching all my random walk demos.
Categorías
Más información sobre Markov Chain Models en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!