MATLAB: Simple Matrix Reduction Question. Ones and Zeros.

matrixonesreductionreshaperesizesimplezeros

I have a NxM matrix full of ones and zeros. Each row (1:N) has exactly one, one, somewhere along of the columns. I want to make a matrix Nx1 which shows which columns the ones appeared in. Example:
A = [ 0 0 0 1; 1 0 0 0; 0 1 0 0; 0 0 1 0]
B = [ 4 ; 1 ; 2 ; 3 ]
Should be very simple?

Best Answer

[rows,cols]=find(A==1);
[C,I]=sort(rows);
B=cols(I)
Related Question