Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.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_Visualization - Fatal编程技术网

Matlab 如何可视化二维矩阵中每个非零元素的跟踪?

Matlab 如何可视化二维矩阵中每个非零元素的跟踪?,matlab,visualization,Matlab,Visualization,我有一个2D矩阵,其中的元素不是1就是0 随着时间的推移,该矩阵会随着其他变量的变化而更新。该更新使得矩阵的“1”元素在坐标中移动,以将自身聚合到特定位置(可能是2D矩阵的中心) 因此,我想跟踪每个“1”元素向中心的运动。如何实现这一点?此答案将帮助您可视化您的点及其移动历史,但它无法处理非零元素的跟踪 让我们从示例数据开始: %% // sample data nMax = 10 ; %// Max size of matrice M0 = randi([0 1]

我有一个2D矩阵,其中的元素不是1就是0

随着时间的推移,该矩阵会随着其他变量的变化而更新。该更新使得矩阵的“1”元素在坐标中移动,以将自身聚合到特定位置(可能是2D矩阵的中心)


因此,我想跟踪每个“1”元素向中心的运动。如何实现这一点?

此答案将帮助您可视化您的点及其移动历史,但它无法处理非零元素的跟踪

让我们从示例数据开始:

%% // sample data
nMax = 10 ;               %// Max size of matrice
M0 = randi([0 1],nMax) ;  %// populate with random "0" and "1"

[x,y] = find(M0==1) ;     %// find coordinates of "1"s
npt = numel(x) ;          %// how many have we got
然后我们画出初始状态。我在每个
1
中使用了两个图形对象:一个带有特定标记的单点显示,用于显示轨迹的“头部”(该点的最后位置),一个虚线(没有标记)用于显示历史轨迹

%% // Display initial state
hf = figure ; hax = axes('Nextplot','Add') ;

for ip = 1:npt
    %// draw the lasp point (the "head")
    hp(ip) = plot( x(ip) , y(ip) , 'Marker','o' , 'LineStyle','none' ) ;
    %// draw the history line (empty at the moment, will populate later)
    hl(ip) = plot( x(ip) , y(ip) , 'Marker','none' , 'LineStyle',':' ) ;
end

set( hax , 'XLim',[0 nMax],'YLim',[0 nMax]) %// to fix axis limits
然后是动画本身。为了移动这些点,在每次动画迭代中,我都会向最后一个坐标添加一小部分。您必须用自己的坐标更新替换该零件。
然后将新坐标与旧坐标连接起来,并更新每个图形对象:

%% // now do the animation
nHist = 30 ; %// number of history point to display on the trace

for animStep = 1:100
    %//                  Movement engine
    %// ---------------------------------------------------------
    %// Replace this block with your own point coordinate update
    x = [ x , x(:,end) + randi([-1 1],npt,1)/10 ] ;
    y = [ y , y(:,end) + randi([-1 1],npt,1)/10 ] ;
    x(x<0) = 0 ; x(x>nMax) = nMax ;   %// keep data within boundaries
    y(x<0) = 0 ; y(y>nMax) = nMax ;
    %// ---------------------------------------------------------


    %% // update display
    for ip = 1:npt
        %// update "Head" point
        set( hp(ip) , 'XData',x(ip,end)  ,'YData',y(ip,end) ) 

        %// update history trace
        idxTrace = max(1,size(x,2)-nHist):size(x,2) ;
        set( hl(ip) , 'XData',x(ip,idxTrace)  ,'YData',y(ip,idxTrace) ) 
    end
    drawnow
    pause(0.1)

end
%%//现在开始制作动画
nHist=30;%//要在跟踪上显示的历史点的数量
对于animStep=1:100
%//运动引擎
%// ---------------------------------------------------------
%//使用自己的点坐标更新替换此块
x=[x,x(:,end)+randi([-11],npt,1)/10];
y=[y,y(:,end)+randi([-11],npt,1)/10];
x(xnMax)=nMax;%//将数据保持在边界内
y(xnMax)=nMax;
%// ---------------------------------------------------------
%%//更新显示
对于ip=1:npt
%//更新“头部”点
设置(hp(ip),“扩展数据”,x(ip,结束),“YData”,y(ip,结束))
%//更新历史记录跟踪
idxTrace=max(1,大小(x,2)-nHist:size(x,2);
集合(hl(ip),'XData',x(ip,idxTrace),'YData',y(ip,idxTrace))
结束
刷新屏幕
暂停(0.1)
结束
这将产生以下结果:

您可以调整变量
nHist
,以更改要显示的历史记录点数


如果矩阵中的“头”标记太多,您也可以将其更改为较小的标记。

此答案将帮助您可视化点及其移动历史,但它不会处理非零元素的跟踪

让我们从示例数据开始:

%% // sample data
nMax = 10 ;               %// Max size of matrice
M0 = randi([0 1],nMax) ;  %// populate with random "0" and "1"

[x,y] = find(M0==1) ;     %// find coordinates of "1"s
npt = numel(x) ;          %// how many have we got
然后我们画出初始状态。我在每个
1
中使用了两个图形对象:一个带有特定标记的单点显示,用于显示轨迹的“头部”(该点的最后位置),一个虚线(没有标记)用于显示历史轨迹

%% // Display initial state
hf = figure ; hax = axes('Nextplot','Add') ;

for ip = 1:npt
    %// draw the lasp point (the "head")
    hp(ip) = plot( x(ip) , y(ip) , 'Marker','o' , 'LineStyle','none' ) ;
    %// draw the history line (empty at the moment, will populate later)
    hl(ip) = plot( x(ip) , y(ip) , 'Marker','none' , 'LineStyle',':' ) ;
end

set( hax , 'XLim',[0 nMax],'YLim',[0 nMax]) %// to fix axis limits
然后是动画本身。为了移动这些点,在每次动画迭代中,我都会向最后一个坐标添加一小部分。您必须用自己的坐标更新替换该零件。
然后将新坐标与旧坐标连接起来,并更新每个图形对象:

%% // now do the animation
nHist = 30 ; %// number of history point to display on the trace

for animStep = 1:100
    %//                  Movement engine
    %// ---------------------------------------------------------
    %// Replace this block with your own point coordinate update
    x = [ x , x(:,end) + randi([-1 1],npt,1)/10 ] ;
    y = [ y , y(:,end) + randi([-1 1],npt,1)/10 ] ;
    x(x<0) = 0 ; x(x>nMax) = nMax ;   %// keep data within boundaries
    y(x<0) = 0 ; y(y>nMax) = nMax ;
    %// ---------------------------------------------------------


    %% // update display
    for ip = 1:npt
        %// update "Head" point
        set( hp(ip) , 'XData',x(ip,end)  ,'YData',y(ip,end) ) 

        %// update history trace
        idxTrace = max(1,size(x,2)-nHist):size(x,2) ;
        set( hl(ip) , 'XData',x(ip,idxTrace)  ,'YData',y(ip,idxTrace) ) 
    end
    drawnow
    pause(0.1)

end
%%//现在开始制作动画
nHist=30;%//要在跟踪上显示的历史点的数量
对于animStep=1:100
%//运动引擎
%// ---------------------------------------------------------
%//使用自己的点坐标更新替换此块
x=[x,x(:,end)+randi([-11],npt,1)/10];
y=[y,y(:,end)+randi([-11],npt,1)/10];
x(xnMax)=nMax;%//将数据保持在边界内
y(xnMax)=nMax;
%// ---------------------------------------------------------
%%//更新显示
对于ip=1:npt
%//更新“头部”点
设置(hp(ip),“扩展数据”,x(ip,结束),“YData”,y(ip,结束))
%//更新历史记录跟踪
idxTrace=max(1,大小(x,2)-nHist:size(x,2);
集合(hl(ip),'XData',x(ip,idxTrace),'YData',y(ip,idxTrace))
结束
刷新屏幕
暂停(0.1)
结束
这将产生以下结果:

您可以调整变量
nHist
,以更改要显示的历史记录点数


如果矩阵中的“头”标记太多,您也可以将其更改为较小的标记。

此答案将帮助您可视化点及其移动历史,但它不会处理非零元素的跟踪

让我们从示例数据开始:

%% // sample data
nMax = 10 ;               %// Max size of matrice
M0 = randi([0 1],nMax) ;  %// populate with random "0" and "1"

[x,y] = find(M0==1) ;     %// find coordinates of "1"s
npt = numel(x) ;          %// how many have we got
然后我们画出初始状态。我在每个
1
中使用了两个图形对象:一个带有特定标记的单点显示,用于显示轨迹的“头部”(该点的最后位置),一个虚线(没有标记)用于显示历史轨迹

%% // Display initial state
hf = figure ; hax = axes('Nextplot','Add') ;

for ip = 1:npt
    %// draw the lasp point (the "head")
    hp(ip) = plot( x(ip) , y(ip) , 'Marker','o' , 'LineStyle','none' ) ;
    %// draw the history line (empty at the moment, will populate later)
    hl(ip) = plot( x(ip) , y(ip) , 'Marker','none' , 'LineStyle',':' ) ;
end

set( hax , 'XLim',[0 nMax],'YLim',[0 nMax]) %// to fix axis limits
然后是动画本身。为了移动这些点,在每次动画迭代中,我都会向最后一个坐标添加一小部分。您必须用自己的坐标更新替换该零件。
然后将新坐标与旧坐标连接起来,并更新每个图形对象:

%% // now do the animation
nHist = 30 ; %// number of history point to display on the trace

for animStep = 1:100
    %//                  Movement engine
    %// ---------------------------------------------------------
    %// Replace this block with your own point coordinate update
    x = [ x , x(:,end) + randi([-1 1],npt,1)/10 ] ;
    y = [ y , y(:,end) + randi([-1 1],npt,1)/10 ] ;
    x(x<0) = 0 ; x(x>nMax) = nMax ;   %// keep data within boundaries
    y(x<0) = 0 ; y(y>nMax) = nMax ;
    %// ---------------------------------------------------------


    %% // update display
    for ip = 1:npt
        %// update "Head" point
        set( hp(ip) , 'XData',x(ip,end)  ,'YData',y(ip,end) ) 

        %// update history trace
        idxTrace = max(1,size(x,2)-nHist):size(x,2) ;
        set( hl(ip) , 'XData',x(ip,idxTrace)  ,'YData',y(ip,idxTrace) ) 
    end
    drawnow
    pause(0.1)

end
%%//现在开始制作动画
nHist=30;%//要在跟踪上显示的历史点的数量
对于animStep=1:100
%//运动引擎
%// ---------------------------------------------------------
%//使用自己的点坐标更新替换此块
x=[x,x(:,end)+randi([-11],npt,1)/10];
y=[y,y(:,end)+randi([-11],npt,1)/10];
x(xnMax)=nMax;%//将数据保持在边界内
y(xnMax)=nMax;
%// ---------------------------------------------------------
%%//更新显示
对于ip=1:npt
%//更新“头部”点
设置(hp(ip),“扩展数据”,x(ip,结束),“YData”,y(ip,结束))
%//更新历史记录跟踪
idxTrace=max(1,大小(x,2)-nHist:size(x,2);
集合(hl(ip),'XData',x(ip,idxTrace),'YData',y(ip,idxTrace))
结束
刷新屏幕
暂停(0