How to apply Random walks ?
显示 更早的评论
If I'ave axes (x,y) and i want to apply random walk on it.is there a function in matlab stands for this .
回答(3 个)
John D'Errico
2012-5-11
5 个投票
xy = cumsum(-1 + 2*round(rand(1000,2)),1);
Why do you need a function?
Image Analyst
2012-5-11
编辑:Image Analyst
2020-1-22
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 个评论
Stelios Fanourakis
2019-7-28
Image Analyst. Can I apply this Random Walk algorithm to a 2d image?
Image Analyst
2019-7-28
Not sure what you mean, but sure, give it a try.
Stelios Fanourakis
2019-7-28
Is this automatic or semi automatic? I guess it needs the user to define seeds. Is there an option for this to be automated?
Image Analyst
2019-7-29
My code does not ask the user for any "seeds". My code asks the user for the number of steps. From then it's automatic, though of course you could alter any of the variables you want.
Richard Willey
2012-5-11
1 个投票
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 个评论
Lucio Biondaro
2022-10-5
why do you use rand by removing 0.5? I can't understand why a step is chosen that is at most 0.5. Thank you so much for an answer
Image Analyst
2022-10-5
@Lucio Biondaro rand() gives a number in the range of 0-1. If you want a number in the range of -0.5 to +0.5 you must subtract 0.5 from the result rand() gives you.
Lucio Biondaro
2022-10-5
thank you for answering. I rephrase the question: I understand what you are saying, I wonder why I use a maximum advance of 0.5 and not 1 or another value. I think I have not quite understood something about the random walk theory or I am missing something.
Image Analyst
2022-10-5
You can use whatever you want. You're defining the experiment so you can specify whatever step size you want. Why do you think it always has to be a step size (delta x or delta y) of exactly 1? There is no requirement for that. You can use whatever you want.
Lucio Biondaro
2022-10-6
There is actually no reason to choose 1 or 0.5. I had imagined that the randn function could be used with average value the average distance of the displacement. But I believe such a model would no longer be a random walk.
Image Analyst
2022-10-6
类别
在 帮助中心 和 File Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!