MATLAB: FInd the positions of the first nonzero entry of each column.

findhomework not originally tagged as homework

For example:
3. Let ? = (0200;0405;7100). We define ??(?) = (
3
1
4
2
).
??(?) is a column vector containing the positions of the first nonzero entry of each column.
Write a Matlab script whose input is a matrix ? and output is ??(?)

Best Answer

A=[0 2 0 0; 0 4 0 5; 7 1 0 0 ];
rc = size(A,1);%size of row
cc = size(A,2);%size of column
disp('The positions of the first nonzero entry of each column are PC = ');
for i=1:cc
V = A(:,i);%to get column by column
count =0;
countzero = 0 ;
for j = 1:rc
if(V(j) ~= 0)
count = count+1;
if(count == 1)
disp(j);
end
else if( V(j) == 0)
countzero = countzero +1;
if(countzero == rc)
disp(0);%there are no nonzero
end
end
end
end
end
i solve it after trying so many way.:)