Q:

Best way to flatten a 2D matrix to 1D when sliced from a 3D matrix variable

I have a 3D matrix in Matlab to store a sequence of 2D arrays. I'm having to find the maximal value and its row and column indices, which is pretty straightforward for a single variable that holds a 2D array as in
A = rand(10,10);
[m,i] = max(A(:));
[I,J] = ind2sub( size(A) , i )

The trouble is that I cannot use this syntax for the 3D matrix
A = rand(10,10,3);
[m,i] = max( A(:,:,1)(:) );
[I,J] = ind2sub(size( A(:,:,1) ), i )

Error: ()-indexing must appear last in an index expression.

I could create a temporary variable to store the 2D slice, but I'd thought I'd see if there's a better means of doing this, maybe making a call to reshape? Is there any way to use the simple linearizing/flattening operator (:) in this context?

A:

Here's what I'd do:
[B i]=max(reshape(A,[],size(A,3)));
[II,JJ]=ind2sub(size(A),i );

The only limitation is that it wont treat well cases where there is more than one max per 2D slice.

