Sorting a sparse matrix according to matrix entries

4 ビュー (過去 30 日間)
Milos
Milos 2023 年 3 月 15 日
コメント済み: Milos 2023 年 3 月 16 日
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 Center および File ExchangeShifting and Sorting Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by