[英]How can I visualize the tracking of every non-zero elements in a 2D matrix?
該答案將幫助您可視化點及其運動歷史,但不能處理非零元素的跟蹤 。
讓我們從示例數據開始:
%% // 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.