Hi Patrick, I think this is what you want to do:
arrSz = [5 6 7];
cube1 = 10*rand(arrSz);
[vals1, idxs1] = max(cube1,,3);
idxsMask = bsxfun(@eq,reshape(1:arrSz(3), 1,1,), idxs1);
cube1(idxsMask) = -inf;
[vals2, idxs2] = max(cube1,,3);
The trick here is the bsxfun() call. I make a set of indices along the 3rd dimension, then bsxfun does its magic asking if the 2D matrix idxs1 matches that array. bsxfun() is a strange function if you haven't seen it before, but once you understand what it does it can be very useful. And very efficient - much faster than the loop.
Walter's question is a valid one... what if the max value isn't unique along the 3rd dimension? Should your second call to max() return the next index that had the same value as the first call? Or should the first call turn all elements that had the max value to -inf?
If you want to do this second way, simply replace the idxsMask line above with the following:
idxsMask = bsxfun(@eq,vals1,cube1);