MATLAB: Finding nonzero elements in matrix

non zero elements in a matrix

Hello,
I have a matrix with the values of either zero or one; I want to find non zero elements in every coloumn and locate the indices of each non zero element in every coloumn of a new matrix. For example, let's say my original matrix looks like this:
a =
0 1 1
0 0 1
1 1 1
I want the indices of non zero element in a new matrix look like this:
b =
0 1 1
0 0 2
3 3 3
How can I do it? I tried with the find function but without any success.
Thanks in advance,
Tilek

Best Answer

iwant = zeros(size(A)) ;
for i = 1:size(A,1)
if any(A(i,:))
iwant(i,A(i,:)~=0) = i ;
end
end