필터 지우기
필터 지우기

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
DEVANAND
DEVANAND 2014년 8월 18일
Sorry this is not what I wanted. When I checked it shows wrong output.
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