Finding the column index for the 1 in each row of a matrix

Viewed 195

I have the following matrix in Matlab:

M = [0 0 1
     1 0 0
     0 1 0
     1 0 0
     0 0 1];

Each row has exactly one 1. How can I (without looping) determine a column vector so that the first element is a 2 if there is a 1 in the second column, the second element is a 3 for a one in the third column etc.? The above example should turn into:

M = [ 3
      1
      2
      1
      3];
3 Answers
Related