行列内の全ての要素を​比較して、降順に配列​を並べ替える方法

14 views (last 30 days)
朋貴 熊田
朋貴 熊田 on 14 Mar 2024
Commented: 朋貴 熊田 on 15 Mar 2024
行列の配列を降順・昇順に並べ替えるのにsort関数があります。しかし、この関数では列方向や行方向の並べ替えのみであり、行列内全ての要素を加味した上での並べ替えを行う事が不可能であります。従って、行列の行方向、列方向を無視し、単純に全ての要素を降順に並べ替える方法をご教授頂けると幸いです。
例えば以下のような行列が合った時
A = [3 6 5; 7 -2 4; 1 0 -9]
A = 3×3
3 6 5 7 -2 4 1 0 -9
sort関数を使うとBになりますが
B=sort(A,'descend')
B = 3×3
7 6 5 3 0 4 1 -2 -9
以下のDのように、全ての行列要素を加味した上で並べ替えたいです。
C=[7 6 5 4 3 1 0 -2 -9]
C = 1×9
7 6 5 4 3 1 0 -2 -9
D=[7 6 5 ; 4 3 1; 0 -2 -9]
D = 3×3
7 6 5 4 3 1 0 -2 -9

Accepted Answer

Atsushi Ueno
Atsushi Ueno on 14 Mar 2024
A = [3 6 5; 7 -2 4; 1 0 -9];
E = sort(A(:),'descend')' % 転置の理由:単に行数を少なくする為
E = 1×9
7 6 5 4 3 1 0 -2 -9
F = reshape(E,size(A))' % 転置の理由:MATLABは列優先だから
F = 3×3
7 6 5 4 3 1 0 -2 -9
  1 Comment
朋貴 熊田
朋貴 熊田 on 15 Mar 2024
ご返信ありがとうございます。

Sign in to comment.

More Answers (0)

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!