I want to sort rows of matrix according to a number of non zero elements in the rows consider the following example where
a = [0 0 2;2 1 4;2 5 0]
but a should be
a =
2 1 4
2 5 0
0 0 2
in the end , here is what i have come up with
for i = 1:3
vec(i) = sum(a(i,:)==0);%to get number of nonzero elementsin each row
end
a = [a vec.']
a = sortrows(a,4) % sorting according to number of nonzero elements
a = [a(:,1:3)]
the code above works but does anyone have a more elegant way ?