Sorting a sparse matrix according to matrix entries
3 views (last 30 days)
Show older comments
I have a sparse matrix where I would like to sort the "list" according to the entries of the matrix. That is to say the first item in the list of the sorted sparse matrix would be the matrix position of the smallest vlaue of said matrix. To give an example;
spX =
(3,2) 0.4190
(2,4) 0.3872
(6,4) 0.1841
(5,5) 0.9246
(6,6) 0.6273
(7,6) 0.0216
(4,7) 0.5755
(10,9) 0.1069
(2,10) 0.9397
(6,10) 0.9456
% A sparse matrix I have. After sorting one would hope to obtain;
sort(spX) =
(7,6) 0.0216
(10,9) 0.1069
(6,4) 0.1841
(2,4) 0.3872
(3,2) 0.4190
(4,7) 0.5755
(6,6) 0.6273
(5,5) 0.9246
(2,10) 0.9397
(6,10) 0.9456
%this is not the output that we get when we use sort.
I have tried the rowsort and sort function but neither yielded any fruit
Many Thanks,
Milos
0 Comments
Accepted Answer
Walter Roberson
on 15 Mar 2023
[r, c, s] = find(spX);
[s, idx] = sort(s);
out = [r(idx), c(idx), s]
You will not be able to get a sorted sparse matrix. You could reconstruct a sparse matrix with sparse(r(idx), c(idx), s) but it would just end up putting the entries back where they were before.
More Answers (0)
See Also
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!