Hello, I'm not sure how to best describe my intention, so I'll use an example:
Say I have a 3D-array:
A(:,:,1) = [1 2 ; 3 4]
A(:,:,2) = [5 6 ; 7 8]
and a 2x2 matrix:
B = [1 2 ; 1 2]
I want MATLAB to give me a 2x2 matrix comprised of elements of A, where the 3rd-dimension position of the element that is picked is indexed by the matrix B. So, in this example:
C = A(:,:,B)
should return [1 6 ; 3 8], because it picks the elements of A(:,:,1) in the first column, and the elements of A(:,:,2) in the second column of the 2x2 matrix.
I could see this potentially being achieved with linear indexing, but is there a cleaner way?
Thanks!

 채택된 답변

James Tursa
James Tursa 2016년 3월 30일
편집: James Tursa 2016년 3월 30일

0 개 추천

Try this using linear indexing as you suggested:
z = size(A);
mn = z(1) * z(2);
x = (1:mn)' + (B(:)-1)*mn;
C = reshape(A(x),z(1),z(2));

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

질문:

2016년 3월 30일

편집:

2016년 3월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by