Picking values from n-d array along 3rd dimension
1 次查看(过去 30 天)
显示 更早的评论
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,
0 个评论
采纳的回答
Andrei Bobrov
2013-6-11
编辑:Andrei Bobrov
2013-6-11
[ii,jj] = ndgrid(1:m,1:n);
ijk = sub2ind(size(A),ii,jj,B);
C = A(ijk);
更多回答(2 个)
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)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!