Sorting a sparse matrix according to matrix entries
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Walter Roberson
2023-3-15
[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.
更多回答(0 个)
另请参阅
类别
在 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!