reflecting random walk in matlab? -
this question has answer here:
i have array of 10 vectors 'x' below (for simulating 1d random walk):
r=rand(10,1000); r(r>.5)=1; r(r<=.5)=-1; x=cumsum(r);
the image of 1 vector like:
if consider 2 values in sequence , +10 , -10, reflect sequence 'x' when reaches values. how achieve this?
before answering question, should point code broken. default cumsum
accumulate data across first dimension, change behavior should specify dim
parameter:
x = cumsum(r,2);
and, answering question, invert data above threshold
:
threshold = 10; nsteps = ceil( max(abs(x(:))) / (2*threshold) - 0.5 ); ii = 1:nsteps ind = abs(x) > 10; x(ind) = 20 * sign(x(ind)) - x(ind); end
Comments
Post a Comment