help in adding a descending order a symetric matric
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
ana take
am 16 Jan. 2017
Kommentiert: ana take
am 17 Jan. 2017
Hello! I want to put in a descending order just the part upper the main diagonal in a symmetric matrix and to get the index of those elements. What should I change in this code?
function [values,i,j] = max(A,n)
[a, linIdx] = sort(A(:),'descend');
values = a(1:n);
[i,j] = ind2sub(size(A),linIdx(1:n));
1 Kommentar
Walter Roberson
am 17 Jan. 2017
Please do not call your function "max" as that interferes with using the key MATLAB routine "max"
Akzeptierte Antwort
John Chilleri
am 16 Jan. 2017
Bearbeitet: John Chilleri
am 16 Jan. 2017
Hello,
Here's a very inelegant solution, but it does the job. I'm making all values below the main diagonal -Inf so that they're guaranteed to not be apart of the a(1:n) when you sort, assuming your original matrix does not contain -Infinities (this also includes the main diagonal, I can change it to exclude it if necessary).
function [values,i,j] = whateveryouwanttocallit(A,n)
low = tril(A-Inf,-1)+1;
A = low.*A;
[a, linIdx] = sort(A(:),'descend');
values = a(1:n);
[i,j] = ind2sub(size(A),linIdx(1:n));
end
Hope this helps!
0 Kommentare
Weitere Antworten (1)
Walter Roberson
am 17 Jan. 2017
See https://www.mathworks.com/matlabcentral/newsreader/view_thread/278808 for information on extracting the upper diagonal.
However, the question arises of which index you are looking for. Are you looking for an index relative to the vector of extracted values, or are you looking for the index into the original matrix?
Siehe auch
Kategorien
Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!