read multiple points from matrix

15 次查看(过去 30 天)
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?

采纳的回答

Star Strider
Star Strider 2021-5-31
Use the sub2ind function to create linear indices from the subscripts —
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)
I = 1×3
1 52 205
P = A(I)
P = 1×3
0.8620 0.3396 0.2798
Check = [A(X(1),Y(1)), A(X(2),Y(2)), A(X(3),Y(3))]
Check = 1×3
0.8620 0.3396 0.2798
If you want a different result, please describe it in some detail.
.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by