Extract submatrix from matrix with particular indexing
Afficher commentaires plus anciens
I have 3D-array A of size (m,n,p) and matrix B of size (m,n) I want C of size (m,n) such as C(i,j)=A(i,j,b_{i,j})
Is there a simple way to do this without using a loop?
Thank you for helping.
Réponses (1)
Roger Stafford
le 10 Jan 2016
AT = reshape(A,m*n,[]);
C = reshape(AT((1:m*n).'+m*n*(B(:)-1)),m,n);
3 commentaires
Coriolis
le 10 Jan 2016
Roger Stafford
le 10 Jan 2016
It just occurred to me that only one line is needed:
C = reshape(A((1:m*n).'+m*n*(B(:)-1)),m,n);
Star Strider
le 10 Jan 2016
I’m not surprised that you understand it!
+1
(I’m also deleting my Answer.)
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!