MATLAB: How to modify the 1D random walk sequence to a reflecting type random walk

random walk

I have a sequence 'x' generated as below (for simulating 1D random walk):
r=rand(1,1000);
r(r>.5)=1;
r(r<=.5)=-1;
x=cumsum(r);
If I consider 2 values in the sequence , say +10 and -10, then I would like to reflect the sequence 'x' when it reaches those values. How to achieve this?

Best Answer

Try this
logicalIndexes = x > 10
x(logicalIndexes) = 10 - x(logicalIndexes);
logicalIndexes = x < -10
x(logicalIndexes) = -10 - x(logicalIndexes);