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:

enter image description here

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

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -