Extract submatrix from matrix with particular indexing
이전 댓글 표시
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.
답변 (1개)
Roger Stafford
2016년 1월 10일
AT = reshape(A,m*n,[]);
C = reshape(AT((1:m*n).'+m*n*(B(:)-1)),m,n);
댓글 수: 3
Coriolis
2016년 1월 10일
Roger Stafford
2016년 1월 10일
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
2016년 1월 10일
I’m not surprised that you understand it!
+1
(I’m also deleting my Answer.)
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!