MATLAB: Sorting array with condition (paths sorting)

matrixsortsort function

i want sort function to sort the element of matrix except the 1&2 column still fixed. depending at the length of each row (except 0)
like that
a=[1 2 5 2 1 0 0
1 2 3 4 0 0 0
1 2 4 0 0 0 0
1 3 2 0 0 0 0
1 3 3 1 4 0 0
1 3 4 5 0 0 0
2 1 4 5 0 0 0
2 1 3 0 0 0 0]
the first row contain 3 element (after 1&2 column except 0)
the second row contain 2 element
the third row contain 1 element
after sorting
a= [ 1 2 4 0 0 0 0
1 2 3 4 0 0 0
1 2 5 2 1 0 0
1 3 2 0 0 0 0
1 3 4 5 0 0 0
1 3 3 1 4 0 0
2 1 3 0 0 0 0
2 1 4 5 0 0 0]

Best Answer

b=[a,sum(logical(a),2)];
result=sortrows(b,[1,2,size(b,2)]);
result(:,end)=[]
Related Question