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

行列の配列を降順・昇順に並べ替えるのに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

 Respuesta aceptada

Atsushi Ueno
Atsushi Ueno el 14 de Mzo. de 2024

1 voto

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 comentario

朋貴 熊田
朋貴 熊田 el 15 de Mzo. de 2024
ご返信ありがとうございます。

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Shifting and Sorting Matrices en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 14 de Mzo. de 2024

Comentada:

el 15 de Mzo. de 2024

Community Treasure Hunt

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

Start Hunting!