0

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

enter image description here

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

So I would like to track the motion of each '1' elements towards the centre. How can I realize this?

Devanand T
  • 63
  • 5
  • 2
    You must have some equation or algorithm that predicts the location of each 1 at the next time step, I assume. Why don't you just save these time steps? Maybe I don;'t understand the problem – TheodorBecker Aug 03 '15 at 07:46
  • 1
    You can use timeseries as @TheodorBecker said or try this way: in your algorithm you have '1'. It moves at other place. Save some value, for example '-1' at previous place (not zero). This '-1' must works like '0' value (any new point can gets up at this place). But you set different colours for it when create plot. So, points will be black, tracks - red, for example. – Mikhail_Sam Aug 03 '15 at 08:22
  • 1
    Are you looking for something [animated](http://stackoverflow.com/questions/11051307/approaches-to-create-a-video-in-matlab), or a 2d-Plot? Also, would it be possible to track each "one" as a single point with x-position and y-position? – Martin J.H. Aug 03 '15 at 08:26
  • @MartinJ.H. : Yes I am looking for something animated. The path of track must be traced as lines. – Devanand T Aug 03 '15 at 08:49
  • 1
    If you want the history of each point, you may have to manage a graphic objet and an array for each single point. How many `1` are you talking about ... and for how long (how many animation steps or frames ?) – Hoki Aug 03 '15 at 08:58
  • @Hoki actually there are ~10,000 `1`s. The animation will be around ~1000000 iterations. – Devanand T Aug 03 '15 at 10:07
  • frames are much lesser. – Devanand T Aug 03 '15 at 10:08
  • 1
    wow, I based my answer on your figure with `1000` non-zeros elements. I tried it and it still run ok, but things may be a bit sluggish with 2x10,000 graphic objects. You may have to skip/average some of the iterations in less frames indeed. – Hoki Aug 03 '15 at 10:16

1 Answers1

3

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:

animpoint

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.

Hoki
  • 11,637
  • 1
  • 24
  • 43