Adding Bias To Random Walk
显示 更早的评论
How would add a bias or probability of 34%, 22%, 22%, and 22% for each of the four directions in the attached random walk code?
3 个评论
TADA
2019-1-14
take a look at that file exchange:
https://www.mathworks.com/matlabcentral/fileexchange/22003-random-walks-in-matlab
Kelly McGuire
2019-1-14
TADA
2019-1-14
Sorry, I was sure there was bias implementation there...
采纳的回答
更多回答(1 个)
Walter Roberson
2019-1-14
There was a recent question in which someone asked for going immediately back to be prohibitted and for the probability of going forward to be doubled. My suggestion then was:
dd = randi(4)
for ss = 1:500
rr = rand;
if rr < 1/4
dd = 1+mod(dd,4) %next higher direction
elseif rr < 1/2
dd = 1+mod(dd-2,4) %next lower direction
end % 50 percent stays same direction
if dd==1
yy=yy+1; %north
elseif dd==2
xx=xx+1; %east
elseif dd==3
yy=yy-1; %south
else
xx=xx-1; %west
end
plot here probably
end
The initial dd is about picking some initial direction. The directions are numbered 1 (north), 2 (east), 3 (south), 4 (west), and the new direction is computed as a change of 0, -1 or +1 to the current direction (-2 was ruled out by not being permitted to go backwards.) You can modify the rr tests for whatever probabilities you want.
It is not clear to me in your question whether the 34% should be for a particular fixed direction (e.g., prefer to head east), or for a relative direction (e.g., prefer to go straight) ?
2 个评论
Kelly McGuire
2019-1-14
编辑:Kelly McGuire
2019-1-14
Walter Roberson
2019-1-14
randsample() the direction numbers with a weights matrix.
Or construct
dv = [1*ones(1,34), 2*ones(1,22), 3*ones(1,22), 4*ones(1,22)]; %direction 1 is overrepresented
dx = [-1 0 1 0]; dy = [0 1 0 -1];
rand_direction = randi(length(dv), 1, num_steps_needed);
rand_dx = dx(rand_direction);
rand_dy = dy(rand_direction);
x_positions = [initial_x, cumsum(rand_dx)];
y_positions = [initial_y, cumsum(rand_dy)];
类别
在 帮助中心 和 File Exchange 中查找有关 Descriptive Statistics and Visualization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!