简体   繁体   中英

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.

在此处输入图片说明

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).

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

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

Which produces the following:

视点

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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