Plotting non zero elements of an array

12 Ansichten (letzte 30 Tage)
Malte Römer-Stumm
Malte Römer-Stumm am 11 Apr. 2022
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?

Antworten (1)

DGM
DGM am 11 Apr. 2022
Try this instead.
plot(nonzeros(X(temp,:,1)),nonzeros(Y(temp,:,1)), 'r+', 'MarkerSize', 5, 'LineWidth', 2);

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by