# How to apply Random walks ?

27 views (last 30 days)

Show older comments

##### 0 Comments

### Answers (3)

John D'Errico
on 11 May 2012

xy = cumsum(-1 + 2*round(rand(1000,2)),1);

Why do you need a function?

##### 0 Comments

Image Analyst
on 11 May 2012

Edited: Image Analyst
on 22 Jan 2020

Try this nice graphical demo:

% Demo to do a random walk in 2 dimensions.

% User is asked for the number of steps to take.

% By Image Analyst

clc; % Clear the command window.

clearvars;

close all; % Close all figures (except those of imtool.)

workspace; % Make sure the workspace panel is showing.

fontSize = 20;

format compact;

% Ask user for a number of steps to take.

defaultValue = 15;

titleBar = 'Enter an integer value';

userPrompt = 'Enter the number of steps to take: ';

caUserInput = inputdlg(userPrompt, userPrompt, 1, {num2str(defaultValue)});

if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.

integerValue = round(str2num(cell2mat(caUserInput)));

% Check for a valid integer.

if isnan(integerValue)

% They didn't enter a number.

% They clicked Cancel, or entered a character, symbols, or something else not allowed.

integerValue = defaultValue;

message = sprintf('I said it had to be an integer.\nI will use %d and continue.', integerValue);

uiwait(warndlg(message));

end

numberOfSteps = integerValue;

deltax = rand(numberOfSteps) - 0.5;

deltay = rand(numberOfSteps) - 0.5;

xy = zeros(numberOfSteps,2);

for step = 2 : numberOfSteps

% Walk in the x direction.

xy(step, 1) = xy(step, 1) + deltax(step);

% Walk in the y direction.

xy(step, 2) = xy(step, 2) + deltay(step);

% Now plot the walk so far.

xCoords = xy(1:step, 1);

yCoords = xy(1:step, 2);

plot(xCoords, yCoords, 'bo-', 'LineWidth', 2);

hold on;

textLabel = sprintf('%d', step);

text(xCoords(end), yCoords(end), textLabel, 'fontSize', fontSize);

end

% Mark the first point in red.

hold on;

plot(xy(1,1), xy(1,2), 'rs', 'LineWidth', 2, 'MarkerSize', 25);

textLabel = '1';

text(xy(1,1), xy(1,2), textLabel, 'fontSize', fontSize);

grid on;

% Mark the last point in red.

plot(xCoords(end), yCoords(end), 'rs', 'LineWidth', 2, 'MarkerSize', 25);

title('Random Walk', 'FontSize', fontSize);

xlabel('X', 'FontSize', fontSize);

ylabel('Y', 'FontSize', fontSize);

% Enlarge figure to full screen.

set(gcf, 'units','normalized','outerposition',[0 0 1 1]);

% Calculate the distance from the origin.

distanceFromOrigin = hypot(xCoords(end), yCoords(end));

message = sprintf('Done with demo!\nDistance of endpoint from origin = %.3f', distanceFromOrigin);

msgbox(message);

##### 4 Comments

Stelios Fanourakis
on 28 Jul 2019

Image Analyst
on 29 Jul 2019

Richard Willey
on 11 May 2012

MATLAB includes a wide variety of functions that can be used to simulate a random walk. Depending on what precisely you want to do you can use anything from the "rand" function in base MATLAB to bm (a function in Econometric Toolbox to model Brownian motion).

The following file exchange submission includes some good examples that a MathWorks Application Engineer developed to illustrate random walks.

##### 6 Comments

Lucio Biondaro
on 6 Oct 2022

Image Analyst
on 6 Oct 2022

### See Also

### Products

### Community Treasure Hunt

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

Start Hunting!