Filter löschen
Filter löschen

Sorting a sparse matrix according to matrix entries

3 Ansichten (letzte 30 Tage)
Milos
Milos am 15 Mär. 2023
Kommentiert: Milos am 16 Mär. 2023
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

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 15 Mär. 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.
  1 Kommentar
Milos
Milos am 16 Mär. 2023
This is great. Sparse wasn't necessary all I wanted was to get an output that is given by your out varaiable.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by