Sorting the elements of a matrix, Part 3
Ältere Kommentare anzeigen
Suppose I have a matrix A. I want to define a matrix B where, for each row, taking entries in the first column and the second column, sort entries so that the entry in the first col will be higher than the one in the second column, and for the entries in the third and the fourth column, conduct the same operation.
For example, for
A=[1 2 3 4; 5 6 7 8]
we should have
B=[2 1 4 3; 6 5 8 7]
But I need to make it work for an arbitrary (even columns) matrix.
2 Kommentare
Star Strider
am 15 Apr. 2018
Note: The original Question was a (2x2) matrix. The (2x4) matrix is new.
Akzeptierte Antwort
Weitere Antworten (1)
Geoff Hayes
am 15 Apr. 2018
alpedhuez - if you want to sort the rows in descending order, then try
B = sort(A,2,'descend')
1 Kommentar
Kategorien
Mehr zu Shifting and Sorting Matrices finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!