matlab - How can I visualize the tracking of every non-zero elements in a 2D matrix? -
i have 2d matrix in elements either 1 or 0.
as time progresses, matrix gets updated w.r.t. other variables. update such '1' elements of matrix moves through coordinates aggregate particular location (may centre of 2d matrix).
so track motion of each '1' elements towards centre. how can realize this?
this answer visualisation of points , movement history, not handle tracking of non-zeros elements.
let's start sample data:
%% // sample data nmax = 10 ; %// max size of matrice m0 = randi([0 1],nmax) ; %// populate random "0" , "1" [x,y] = find(m0==1) ; %// find coordinates of "1"s npt = numel(x) ; %// how many have got
then draw initial state. used 2 graphic objects per 1
: 1 single point display specific marker show "head" of trace (the last position of point), , 1 dotted fine line (with no markers) show history trace.
%% // display initial state hf = figure ; hax = axes('nextplot','add') ; ip = 1:npt %// draw lasp point (the "head") hp(ip) = plot( x(ip) , y(ip) , 'marker','o' , 'linestyle','none' ) ; %// draw history line (empty @ moment, populate later) hl(ip) = plot( x(ip) , y(ip) , 'marker','none' , 'linestyle',':' ) ; end set( hax , 'xlim',[0 nmax],'ylim',[0 nmax]) %// fix axis limits
then animation itself. move points, @ each animation iteration add small quantity last coordinates. you'll have replace part own coordinate update.
concatenate new coordinate old ones, , update each graphic object:
%% // animation nhist = 30 ; %// number of history point display on trace animstep = 1:100 %// movement engine %// --------------------------------------------------------- %// replace block 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 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 following:
you can adjust variable nhist
change number of history points display.
you can change "head" marker smaller if have many of them in matrix.
Comments
Post a Comment