Sorting a sparse matrix according to matrix entries

4 次查看(过去 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

采纳的回答

Walter Roberson
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.
  1 个评论
Milos
Milos 2023-3-16
This is great. Sparse wasn't necessary all I wanted was to get an output that is given by your out varaiable.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by