read multiple points from matrix
15 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
F Schmid
am 31 Mai 2021
Beantwortet: Star Strider
am 31 Mai 2021
I want to read multiple points out of a matrix. Therefore I have the matrix A, and my indices X and Y for the coordinates. I don't need any other values, only those.
A = rand(50,50);
X = [1 2 5];
Y = [1 2 5];
P = A(X,Y)
the code reads all the values, so I only need the diagonal of the matrix. I could use diag but that takes a lot of time, and I need to be time-efficient. Is there a possible way to do that different, to be faster?
0 Kommentare
Akzeptierte Antwort
Star Strider
am 31 Mai 2021
A = rand(50,50);
X = [1 2 5]; % Assuming These Are The Row Indices
Y = [1 2 5]; % Assuming These Are The Column Indices
I = sub2ind(size(A), X, Y)
P = A(I)
Check = [A(X(1),Y(1)), A(X(2),Y(2)), A(X(3),Y(3))]
If you want a different result, please describe it in some detail.
.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!