How to modify my 1D random walk sequence to a reflecting type random walk?

1 次查看(过去 30 天)
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?

采纳的回答

Image Analyst
Image Analyst 2014-8-17
Try this
logicalIndexes = x > 10
x(logicalIndexes) = 10 - x(logicalIndexes);
logicalIndexes = x < -10
x(logicalIndexes) = -10 - x(logicalIndexes);
  2 个评论
Image Analyst
Image Analyst 2014-8-18
Try this. Every time it's at 10 and tries to go to 11, it goes to 9 instead, so it's "reflected" about 10. Same for -10.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
fontSize = 22;
r = 2*randi(2, 1, 1000)- 3;
x(1) = 0;
for index = 2 : length(r)
x(index) = x(index-1) + r(index);
if x(index) > 10
x(index) = 9;
elseif x(index) < -10
x(index) = -9;
end
end
plot(x, 'b-', 'LineWidth', 2);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
grid on;
ylabel('x', 'FontSize', fontSize);
xlabel('Step Number', 'FontSize', fontSize);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by