Matrix transformation (sorting)
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
the cyclist
2020-2-4
编辑:the cyclist
2020-2-4
i = find(sort(A,'descend'));
j = find(A);
s = numel(A);
T = zeros(s,s);
T(sub2ind([s s],i,j)) = 1;
I hope it's clear what is going on here.
Note that in this case, the pseudoinverse of T is equal to the transpose of T. The transpose is actually the easier way to understand what is happening. If there is a 1 in element (x,y) of T, then there has to be a 1 in element (y,x) of the inverse transformation. (You can think of T and pinv(T) as just "sending" 1's back and forth to the appropriate positions.)
更多回答(1 个)
the cyclist
2020-2-3
编辑:the cyclist
2020-2-3
T = sort(A,'descend')/A
Note that I am only using sort here to define the result you need. The transformation matrix you get as a result doesn't rely on any actual sorting algorithm, and can be applied you your other matrices directly.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!