# MATLAB: Matrix transformation (sorting)

matrix manipulationsorting

I have a sparse matrix containing either 0 or 1, for example :
A = [0; 1; 0];
I would like to find a transformation to sort this matrix, i.e. find another matrix such that:
T*A = [1 ; 0 ; 0]
All the 1 should come first.
In this example, T should be :
T = [0 1 0; 0 0 0; 0 0 0];
I can not use builtin functions such as "sort" because I need to apply this transformations to other matrices.
i.e. once I have calculated T corresponding to the A matrix, I need to apply the same transformation to other (not particularly sorted) matrices.
How can I write an algorithm for more complicated examples ? Is there a builtin function that does this already ?
Best regards,
Peter

• i = find(sort(A,'descend'));j = find(A);s = numel(A);T = zeros(s,s);T(sub2ind([s s],i,j)) = 1;