MATLAB: Ind2sub doesn’t work

ind2sub

Check it out:
>> A = zeros(3);
>> A(2,3) = 1
A =
0 0 0
0 0 1
0 0 0
>> tmp = A'
tmp =
0 0 0
0 0 0
0 1 0
>> [maxVal, maxInd] = max(tmp(:))
maxVal =
1
maxInd =
6
And now the WTF comes:
>> ind2sub(size(tmp), maxInd)
ans =
6
>> ind2sub(maxInd, size(tmp))
ans =
3 3
Seems like function is dead.

Best Answer

You are funny.
ind2sub reshapes the siz first argument to match the number of output variables used (in your case a single output argument), so that the resulting subscripts suffice to fully index the original elements. In your case:
ind2sub(size(tmp), maxInd)
is just the same as:
ind2sub(numel(tmp), maxInd)