MATLAB: What would be the way to use one 3D array as index for other 3D array

3d arrayindexing

Hello, I have two 3D arrays a (3x5x4) and o (1x5x4). What I would like to achieve is to use o array as index for a. The struggle is when I use the function below it would take whole o array when creating new array x (1x5x4), maybe anyone has any idea. I thought about pagefun, but it works with only predefined functions.
a=rand(3,5,4)
o= cat(3,[2 5 1 2 4],[2 5 3 1 5],[2 2 2 4 4],[2 3 1 5 4])
x=a(2,o,:)

Best Answer

The simplest solution is to use a temporary variable:
>> tmp = a(2,:,:);
>> x = tmp(o)
Another option would be to convert the subscript indices into linear indices, but this is not so trivial.