繁体   English   中英

如何可视化2D矩阵中每个非零元素的跟踪?

[英]How can I visualize the tracking of every non-zero elements in a 2D matrix?

我有一个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使用2个图形对象:一个带有特定标记的单点显示,以显示轨迹的“头”(该点的最后位置),以及一个细虚线(没有标记),以显示历史轨迹。

%% // 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来更改将显示的历史记录点数。

如果矩阵中包含过多的“ head”标记,也可以将其更改为较小的标记。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM