MATLAB: Indexing arrays with matrices

arraysindexingMATLABmultidimensionalrepmat

Matt's recent Hump-day challenger has led me ponder a strange form of indexing used in repmat. Consider a 3d array like the following:
A = reshape(1:8,[2 2 2])
A(:,:,1) =
1 3
2 4
A(:,:,2) =
5 7
6 8
Now run repmat and stop at the second last line:
B = A(subs{:});
Putting in the values for subs, this is the same as
B = A([1 1; 2 2],[1; 2],[1 1; 2 2]);
How is MATLAB interpreting indexing like this?

Best Answer

Now that is the kind of wondering that lead me to the project which lead me to the challenger!
I think we can see what is going on by taking it one at a time:
A([1 1; 2 2],:,:)
So this produces the same thing as this:
A([1 2 1 2],:,:) % Read the indexing array as (:).
Now it makes sense: Take - Row one, then Row two, then Row one, then Row two for all higher dimensions (i.e., for each column and page).
Similarly with the other indexing arrays.
REPMAT and BSXFUN are deep funtions in terms of really understanding how they (and consequently, MATLAB indexing in general) work.