How to modify my 1D random walk sequence to a reflecting type random walk?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
DEVANAND
el 17 de Ag. de 2014
Comentada: Image Analyst
el 18 de Ag. de 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 comentarios
Respuesta aceptada
Image Analyst
el 17 de Ag. de 2014
Try this
logicalIndexes = x > 10
x(logicalIndexes) = 10 - x(logicalIndexes);
logicalIndexes = x < -10
x(logicalIndexes) = -10 - x(logicalIndexes);
2 comentarios
Image Analyst
el 18 de Ag. de 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);
Más respuestas (0)
Ver también
Categorías
Más información sobre Sources en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!