行列内の全ての要素を比較して、降順に配列を並べ替える方法
187 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
行列の配列を降順・昇順に並べ替えるのにsort関数があります。しかし、この関数では列方向や行方向の並べ替えのみであり、行列内全ての要素を加味した上での並べ替えを行う事が不可能であります。従って、行列の行方向、列方向を無視し、単純に全ての要素を降順に並べ替える方法をご教授頂けると幸いです。
例えば以下のような行列が合った時
A = [3 6 5; 7 -2 4; 1 0 -9]
sort関数を使うとBになりますが
B=sort(A,'descend')
以下のDのように、全ての行列要素を加味した上で並べ替えたいです。
C=[7 6 5 4 3 1 0 -2 -9]
D=[7 6 5 ; 4 3 1; 0 -2 -9]
0 commentaires
Réponse acceptée
Atsushi Ueno
le 14 Mar 2024
A = [3 6 5; 7 -2 4; 1 0 -9];
E = sort(A(:),'descend')' % 転置の理由:単に行数を少なくする為
F = reshape(E,size(A))' % 転置の理由:MATLABは列優先だから
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!