Plotting non zero elements of an array

조회 수: 16 (최근 30일)
Malte Römer-Stumm
Malte Römer-Stumm 2022년 4월 11일
댓글: Malte Römer-Stumm 2022년 4월 12일
I have one array with x-values (X) and y-values (Y) and I want to plot selected lines of those within a loop.
The arrays X and Y have a shape like this:
482.8410 485.8150 487.6960 487.4730 489.4050 491.1900 491.3960 491.8090 0 0
611.4260 612.8240 617.4750 621.8790 622.2030 623.4460 623.0670 622.8820 0 0
355.5830 355.9290 356.7860 0 0 0 0 0 0 0
0 383.2500 385.3630 387.5590 389.0450 394.2830 396.2180 397.0960 395.4490 395.4630
X and Y have zeros in the same spots, only the non zero values differ. X and Y have a third dim, but for this context only layer 1 is relevant. I want to plot chosen lines individually and only the non zero elements from that line. I use the following plot command:
plot(X(temp,find(X(temp,:,1)>0==1),1),Y(temp,find(Y(temp,:,1)>0==1),1), 'r+', 'MarkerSize', 5, 'LineWidth', 2);
The variable temp is a line vector (i.e. 2 3 4) containing the lines, which are supposed to be plotted. I get the error:
Index in position 2 exceeds array bounds (must not exceed 10).
Error in Bubble_movement_analyser (line 424)
plot(X(temp,find(X(temp,:,1)>0==1),1),Y(temp,find(Y(temp,:,1)>0==1),1), 'r+', 'MarkerSize', 5, 'LineWidth', 2);
Additionally I get the comment, that using logical indexing would improve code perfomance, but I couldn't figure out, how to write that.
Does somebody knows why the Index in position 2 exceeds 10 or how to express this with logical indexing?

답변 (1개)

DGM
DGM 2022년 4월 11일
Try this instead.
plot(nonzeros(X(temp,:,1)),nonzeros(Y(temp,:,1)), 'r+', 'MarkerSize', 5, 'LineWidth', 2);
  댓글 수: 1
Malte Römer-Stumm
Malte Römer-Stumm 2022년 4월 12일
Hi, that works very well, thank you :)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by