How to modify my 1D random walk sequence to a reflecting type random walk?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
DEVANAND
le 17 Août 2014
Commenté : Image Analyst
le 18 Août 2014
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?
0 commentaires
Réponse acceptée
Image Analyst
le 17 Août 2014
Try this
logicalIndexes = x > 10
x(logicalIndexes) = 10 - x(logicalIndexes);
logicalIndexes = x < -10
x(logicalIndexes) = -10 - x(logicalIndexes);
2 commentaires
Image Analyst
le 18 Août 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);
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Sources dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!