I am trying to figure out if there is an easy (and quick) way of accessing elements of a multi-dimensional array according to multi-dimensional array of indices.
For example:
A = reshape(1:8,2,2,2);
indxs = repeat([1],1,2,2);
I would like it to output
Mat = zeros(size(indxs,1),size(A,2),size(A,3));
for i=1:size(A,2)
for j=1:size(A,3)
Mat[:,i,j] = A[indxs[:,i,j],i,j];
end
end
whose output is
[:, :, 1] =
1.0 3.0
[:, :, 2] =
5.0 7.0
all in one go, so I try
A[indxs,[1;2],[1;2]]
but, what I thought would work is off (it adds dimensions by doing all pair-wise combinations):
[:, :, 1, 1, 1] =
1 1
[:, :, 2, 1, 1] =
1 1
[:, :, 1, 2, 1] =
3 3
[:, :, 2, 2, 1] =
3 3
[:, :, 1, 1, 2] =
5 5
[:, :, 2, 1, 2] =
5 5
[:, :, 1, 2, 2] =
7 7
[:, :, 2, 2, 2] =
7 7
Any ideas if what I want exists?