Programming |
Sorting the Data in Each Column
The sort
function sorts matrix elements along a specified dimension. The syntax for the function is
To sort the columns of a matrix, specify 1 as the dimension
argument. To sort along rows, specify dimension
as 2.
This example first constructs a 3-by-5 matrix:
A = rand(3,5) * 10 A = 9.5013 7.6210 6.1543 4.0571 0.5789 2.3114 4.5647 7.9194 9.3547 3.5287 6.0684 0.1850 9.2181 9.1690 8.1317
Sort each column of A
in ascending order:
c = sort(A, 1) c = 2.3114 0.1850 6.1543 4.0571 0.5789 6.0684 4.5647 7.9194 9.1690 3.5287 9.5013 7.6210 9.2181 9.3547 8.1317 issorted(c(:, 1)) ans = 1
Shifting the Location of Matrix Elements | Sorting the Data in Each Row |
© 1994-2005 The MathWorks, Inc.