Suppose i have a vector of values A
And a vector of indeces that may have any number of singleton leading dimensions, e.g.
idx = zeros(1,1,3);
idx(:) = 1:3
I want to get the elements of A specified by the subscripts in idx, and I want the output B to have the same dimensions as idx. In this example, what I want to get is
Instead of the default indexing behaviour which would get me:
I know that i could obtain this by first initializing B to have the same size of idx, i.e.
B = zeros(size(idx));
B(:) = A(idx);
However, in my application, I am developing a toolbox that must be able to work with user-created functions. I can act on idx, which will be fed as an input to the user-created function, but the rest is up to the user and I cannot expect him to write these two lines of code.
Is there any way to achieve this?