Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 创建图形,显示在三个不同时间点的大量二维随机游动在空间中的分布_Matlab_Matlab Figure - Fatal编程技术网

Matlab 创建图形,显示在三个不同时间点的大量二维随机游动在空间中的分布

Matlab 创建图形,显示在三个不同时间点的大量二维随机游动在空间中的分布,matlab,matlab-figure,Matlab,Matlab Figure,基本上我这里有一段代码,我可以用它来生成一个2D随机游动,沿着N个步数,M个游动者离散地游动。我可以把它们都画在同一张图上 clc; clearvars; N = 500; % Length of the x-axis, also known as the length of the random walks. M = 3; % The amount of random walks. x_t(1) = 0; y_t(1) = 0; for m=1:M for n = 1:N % Loop

基本上我这里有一段代码,我可以用它来生成一个2D随机游动,沿着N个步数,M个游动者离散地游动。我可以把它们都画在同一张图上

clc;
clearvars;
N = 500; % Length of the x-axis, also known as the length of the random walks.
M = 3; % The amount of random walks.
x_t(1) = 0;
y_t(1) = 0;
for m=1:M
    for n = 1:N % Looping all values of N into x_t(n).
        A = sign(randn); % Generates either +1/-1 depending on the SIGN of RAND.
        x_t(n+1) = x_t(n) + A;
        A = sign(randn); % Generates either +1/-1 depending on the SIGN of RAND.
        y_t(n+1) = y_t(n) + A;
    end
    plot(x_t, y_t);
    hold on
end
grid on;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0.05, 1, 0.95]);
axis square;
现在,我希望能够创建显示大量位置在空间中分布的图形 e、 g.n=1000名随机步行者,在三个不同的时间点,例如t=100、200和300或任何三个时间点

我不知道该怎么做,我需要把它转换成一个函数,迭代三次,然后存储坐标?我有一个粗略的想法,但在实际执行上还不确定。我认为最安全、最不混乱的方法是使用子图在同一个图中创建所有三个图


感谢您的帮助

如果你想显示这些点中大量点的分布,比如说1000个点,我认为最合适的绘制方法是使用散射作为“点云”。然后为x和y坐标创建一个N点数组,并让它在循环中计算i=1:Nt的坐标,其中Nt将是100、200或300,如您所述。大致如下:

N = 500;
x_t = zeros(N,1);
y_t = zeros(N,1);
Nt = 100;
for tidx = 1:Nt
    x_t = x_t + sign(randn(N,1));
    y_t = y_t + sign(randn(N,1));
end
scatter(x_t,y_t,'k*');
这将为您提供nx和y坐标,生成方式与您提供的示例相同


需要记住的一件事是sign0=0,所以我想有一个不改变坐标的小机会。我不确定你是否打算让步行者站着不动就可以做出这种行为?

为了清晰起见,我将演示一维情况;您只需要为添加的每个维度实现此功能

使用NxM矩阵的M步行者的N型步数

对于绘图,制作第二个NxM矩阵s非常有用,该矩阵s包含每个步骤后更新的位置,其中sN,M给出了N个步骤后walker M的位置

使用cumsum进行矢量化,而不是循环

>> s = cumsum(steps)

s =

     1     1     1     1
     0     2     0     2
     1     1    -1     1
     2     2    -2     2
     3     1    -3     1
若要防止在每一新行之后重新绘制打印,请使用“按住”

输出图如下所示:

此方法可以很好地扩展到二维和三维随机游动。

您可以使用cumsum将过程线性化。基本上,您只需要对由[-1和1]组成的随机矩阵求和

clc;
close all;
M = 50; % The amount of random walks.
steps = [10,200,1000]; % here we analyse the step 10,200 and 1000
cc = hsv(length(steps)); % manage the color of the plot
%generation of each random walk
x = sign(randn(max(steps),M));
y = sign(randn(max(steps),M));
xs = cumsum(x);
xval = xs(steps,:);
ys = cumsum(y);
yval = ys(steps,:);

hold on
for n=1:length(steps)
    plot(xval(n,:),yval(n,:),'o','markersize',1,'color',cc(n,:),'MarkerFaceColor',cc(n,:));
end

legend('10','200','1000')
axis square
grid on;
结果:

编辑:

感谢@LuisMendo回答了我的问题,您可以使用二项分布得到相同的结果:

steps = [10,200,10000];
cc = hsv(length(steps)); % manage the color of the plot
M = 50;
DV = [-1 1];
p = .5; % probability of DV(2)
% Using the @LuisMendo binomial solution:
for ii = 1:length(steps)
    SDUDx(ii,:) = (DV(2)-DV(1))*binornd(steps(ii), p, M, 1)+DV(1)*steps(ii);
    SDUDy(ii,:) = (DV(2)-DV(1))*binornd(steps(ii), p, M, 1)+DV(1)*steps(ii);
end
hold on
for n=1:length(steps)
    plot(SDUDx(n,:),SDUDy(n,:),'o','markersize',1,'color',cc(n,:),'MarkerFaceColor',cc(n,:));
end
legend('10','200','1000')
axis square
grid on;
优势是什么?即使你有大量的步骤,比如1000000步,matlab也能处理。因为在第一种解决方案中,你有一个蛮力解决方案,在第二种情况下,你有一个统计解决方案

clc;
close all;
M = 50; % The amount of random walks.
steps = [10,200,1000]; % here we analyse the step 10,200 and 1000
cc = hsv(length(steps)); % manage the color of the plot
%generation of each random walk
x = sign(randn(max(steps),M));
y = sign(randn(max(steps),M));
xs = cumsum(x);
xval = xs(steps,:);
ys = cumsum(y);
yval = ys(steps,:);

hold on
for n=1:length(steps)
    plot(xval(n,:),yval(n,:),'o','markersize',1,'color',cc(n,:),'MarkerFaceColor',cc(n,:));
end

legend('10','200','1000')
axis square
grid on;
steps = [10,200,10000];
cc = hsv(length(steps)); % manage the color of the plot
M = 50;
DV = [-1 1];
p = .5; % probability of DV(2)
% Using the @LuisMendo binomial solution:
for ii = 1:length(steps)
    SDUDx(ii,:) = (DV(2)-DV(1))*binornd(steps(ii), p, M, 1)+DV(1)*steps(ii);
    SDUDy(ii,:) = (DV(2)-DV(1))*binornd(steps(ii), p, M, 1)+DV(1)*steps(ii);
end
hold on
for n=1:length(steps)
    plot(SDUDx(n,:),SDUDy(n,:),'o','markersize',1,'color',cc(n,:),'MarkerFaceColor',cc(n,:));
end
legend('10','200','1000')
axis square
grid on;