簡體   English   中英

如何可視化2D矩陣中每個非零元素的跟蹤?

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

我有一個2D矩陣,其中的元素為1或0。

在此處輸入圖片說明

隨着時間的流逝,該矩陣會通過其他一些變量進行更新。 更新使得矩陣的“ 1”元素通過坐標移動以將自身聚合到特定位置(可能是2D矩陣的中心)。

因此,我想跟蹤每個“ 1”元素向中心的運動。 我怎么能意識到這一點?

該答案將幫助您可視化點及其運動歷史,但不能處理非零元素的跟蹤

讓我們從示例數據開始:

%% // 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個圖形對象:一個帶有特定標記的單點顯示,以顯示軌跡的“頭”(該點的最后位置),以及一個細虛線(沒有標記),以顯示歷史軌跡。

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

然后是動畫本身。 為了移動這些點,在每次動畫迭代時,我都會在最后一個坐標上添加少量。 您必須用自己的坐標更新替換該零件。
然后,我將新坐標與舊坐標連接起來,並更新每個圖形對象:

%% // 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來更改將顯示的歷史記錄點數。

如果矩陣中包含過多的“ head”標記,也可以將其更改為較小的標記。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM