Azzera filtri
Azzera filtri

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

1 visualizzazione (ultimi 30 giorni)
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?

Risposta accettata

Image Analyst
Image Analyst il 17 Ago 2014
Try this
logicalIndexes = x > 10
x(logicalIndexes) = 10 - x(logicalIndexes);
logicalIndexes = x < -10
x(logicalIndexes) = -10 - x(logicalIndexes);
  2 Commenti
DEVANAND
DEVANAND il 18 Ago 2014
Sorry this is not what I wanted. When I checked it shows wrong output.
Image Analyst
Image Analyst il 18 Ago 2014
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);

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Mathematics in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by