Picking values from n-d array along 3rd dimension
古いコメントを表示
Hi,
Suppose I have A, an m x n x q array with large integer values.
I have B, an m x n array of integer values in the range of 1:q.
I'd like to pick values from A along the 3rd dimension, so that I obtain C, an m x n array in which for every subscript pair (i,j), m>i>1, n>j>1 the following is true:
x = B(i,j)
C(i,j) = A(i,j,x)
Is there an efficient, loop-free way of doing this?
Many thanks,
採用された回答
その他の回答 (2 件)
Roger Stafford
2013 年 6 月 11 日
C = A((1:m*n)'+m*n*(B(:)-1));
C = reshape(C,m,n);
3 件のコメント
Andrei Bobrov
2013 年 6 月 11 日
編集済み: Andrei Bobrov
2013 年 6 月 11 日
A(reshape(1:m*n,m,[]) + m*n*(B-1))
Pal
2013 年 6 月 11 日
Roger Stafford
2013 年 6 月 11 日
A(reshape(1-m*n:0,m,[])+m*n*B)
David Sanchez
2013 年 6 月 11 日
You can assign the value of C without referencing (i,j):
x = B(i,j);
C = A(:,:,x);
And you will have for all i, j that C(i,j) = A(i,j,x)
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!