简体   繁体   English

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

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

I have a 2D matrix in which the elements are either 1 or 0. 我有一个2D矩阵,其中的元素为1或0。

在此处输入图片说明

As as time progresses, this matrix gets updated wrt some other variables. 随着时间的流逝,该矩阵会通过其他一些变量进行更新。 The update is such that the '1' elements of matrix moves through the coordinates to aggregate itself to a particular location (may be centre of the 2D matrix). 更新使得矩阵的“ 1”元素通过坐标移动以将自身聚合到特定位置(可能是2D矩阵的中心)。

So I would like to track the motion of each '1' elements towards the centre. 因此,我想跟踪每个“ 1”元素向中心的运动。 How can I realize this? 我怎么能意识到这一点?

This answer will help you with the visualisation of your points and their movement history, but it does not handle the tracking of your non-zeros elements. 该答案将帮助您可视化点及其运动历史,但不能处理非零元素的跟踪

Let's start with sample data: 让我们从示例数据开始:

%% // 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

Then we draw the initial state. 然后我们绘制初始状态。 I used 2 graphic objects per 1 : One single point display with a specific marker to show the "head" of the trace (the last position of the point), and one dotted fine line (with no markers) to show the history trace. 我每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

Then the animation itself. 然后是动画本身。 To move the points, at each animation iteration I add a small quantity to the last coordinates. 为了移动这些点,在每次动画迭代时,我都会在最后一个坐标上添加少量。 You'll have to replace that part with your own coordinate update. 您必须用自己的坐标更新替换该零件。
Then I concatenate the new coordinate with the old ones, and update each graphic object: 然后,我将新坐标与旧坐标连接起来,并更新每个图形对象:

%% // 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

Which produces the following: 产生以下内容:

视点

You can adjust the variable nHist to change the number of history points you will display. 您可以调整变量nHist来更改将显示的历史记录点数。

You can also change the "head" marker to something smaller if you have too many of them in your matrix. 如果矩阵中包含过多的“ head”标记,也可以将其更改为较小的标记。

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

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