sortrows - Sort rows

Class

@dataset

Syntax

B = sortrows(A)
B = sortrows(A,vars)
B = sortrows(A,'obsnames')
B = sortrows(A,vars,mode)
[B,idx] = sortrows(A)

Description

B = sortrows(A) returns a copy of the dataset array A, with the observations sorted in ascending order by all of the variables in A. The observations in B are sorted first by the first variable, next by the second variable, and so on. The variables in A must be scalar valued (i.e., column vectors) and be from a class for which a sort method exists.

B = sortrows(A,vars) sorts the observations in A by the variables specified by vars. vars is a positive integer, a vector of positive integers, variable names, a cell array containing one or more variable names, or a logical vector.

B = sortrows(A,'obsnames') sorts the observations in A by observation name.

B = sortrows(A,vars,mode) sorts in the direction specified by mode. mode is 'ascend' (the default) or 'descend'. Use [] for vars to sort using all variables.

[B,idx] = sortrows(A) also returns an index vector idx such that B = A(idx,:).

Example

Sort the data in hospital.mat by age and then by last name:

load hospital
hospital(1:5,1:3)
ans = 
            LastName       Sex       Age
 YPL-320    'SMITH'        Male      38 
 GLI-532    'JOHNSON'      Male      43 
 PNI-258    'WILLIAMS'     Female    38 
 MIJ-579    'JONES'        Female    40 
 XLK-030    'BROWN'        Female    49 

hospital = sortrows(hospital,{'Age','LastName'});
hospital(1:5,1:3)
ans = 
            LastName       Sex       Age
 REV-997    'ALEXANDER'    Male      25 
 FZR-250    'HALL'         Male      25 
 LIM-480    'HILL'         Female    25 
 XUE-826    'JACKSON'      Male      25 
 SCQ-914    'JAMES'        Male      25 

See Also

sortrows

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS