如何可视化2D矩阵中每个非零元素的跟踪? [英] How can I visualize the tracking of every non-zero elements in a 2D matrix?

查看:97
本文介绍了如何可视化2D矩阵中每个非零元素的跟踪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个2D矩阵,其中的元素为1或0.

I have a 2D matrix in which the elements are either 1 or 0.

随着时间的流逝,此矩阵随时间更新.其他一些变量.更新后,矩阵的"1"元素将通过坐标移动以将自身聚合到特定位置(可能是2D矩阵的中心).

As as time progresses, this matrix gets updated w.r.t. 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"元素朝向中心的运动.我怎么能意识到这一点?

So I would like to track the motion of each '1' elements towards the centre. 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

然后我们绘制初始状态.我每个1使用2个图形对象:一个带有特定标记的单点显示,以显示轨迹的头"(该点的最后一个位置),以及一个虚线细线(无标记),以显示历史记录跟踪.

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.

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

产生以下内容:

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

You can adjust the variable nHist to change the number of history points you will display.

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

You can also change the "head" marker to something smaller if you have too many of them in your matrix.

这篇关于如何可视化2D矩阵中每个非零元素的跟踪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆