I have three 3D matrices (A,B and C).

Now I need to get a matrix which provides me with the index of the matrix (A,B or C) that has the maximum value at that point. Thus eventually a 3D matrix with ones, twos and threes).

This matrix can be obtained as follows:

BFR(:,:,:,1)=A;

BFR(:,:,:,2)=B;

BFR(:,:,:,3)=C;

[M,i]=max(BFR,[],4)

The problemis that in my final matrix M, all elements for which the values of A,B and C are all equal to NaN (or 0), it gives the index 1 as the maximum.

I want the outcome for that situation to be 0, does anyone know how I can solve this?

Thanks in advance!