Matrix 3D fuzzy binary matlab

advertisements

I have a fuzzy matrix M that size is 3x3x3such that

M(:,:,1)+M(:,:,2)+M(:,:,3)=matrix one %all elements is one
0<=M(:,:,i)<=1; i=1..3

I want to covert the matrix M to binary matrix such that

if (M(p,q,i)>=M(p,q,j)) then M(p,q,i)=1, M(p,q,j)=0

where p,q is position of a element. i,j=1..3

Note that if we already set M(p,q,i)=1 then M(p,q,j) must be 0 (j=1..3, j!=i) because I want to sum of binary matrix M still equals 1 (sum(M,3)=ones(3, 3))

Could you help me perform that idea in matlab? Thank you so much

For example:

M=zeros([3 3 3]);
M(:,:,1) =  [0.2000    0.3000    0.4000;
             0.5000    0.6000    0.7000;
             0.3000    0.2000    0.1000];

M(:,:,2) =  [0.4000    0.1000    0.6000;
             0.1000    0.3000    0.1000;
             0.1000    0.2000    0.1000];

M(:,:,3) =  [0.4000    0.6000         0;
             0.4000    0.1000    0.2000;
             0.6000    0.6000    0.8000];

My expected output is

M_out(:,:,1)=[0        0         0;
              1        1         1
              0        0         0];

M_out(:,:,2)=[1        0         1;
              0        0         0
              0        0         0];

M_out(:,:,3)=[0        1         0;
              0        0         0
              1        1         1];


Let bsxfun & permute duo solve it for a generic and vectorized solution -

%// Get max indices along dim-3
[~,idx] = max(M,[],3);

%// Setup o/p logical array with same size as M and 1s at starting max indices
M_out = bsxfun(@eq,idx,permute(1:size(M,3),[1 3 2]))

Sample run -

>> M
M(:,:,1) =
          0.2          0.3          0.4
          0.5          0.6          0.7
          0.3          0.2          0.1
M(:,:,2) =
          0.4          0.1          0.6
          0.1          0.3          0.1
          0.1          0.2          0.1
M(:,:,3) =
          0.4          0.6            0
          0.4          0.1          0.2
          0.6          0.6          0.8
>> [~,idx] = max(M,[],3);
M_out = bsxfun(@eq,idx,permute(1:size(M,3),[1 3 2]))
M_out(:,:,1) =
     0     0     0
     1     1     1
     0     0     0
M_out(:,:,2) =
     1     0     1
     0     0     0
     0     0     0
M_out(:,:,3) =
     0     1     0
     0     0     0
     1     1     1