www.gusucode.com > stats 源码程序 matlab案例代码 > stats/SortObservationsinDatasetArraysExample.m

    %% Sort Observations in Dataset Arrays  
% This example shows how to sort observations (rows) in a dataset array
% using the command line. You can also sort rows using the Variables editor.   

% Copyright 2015 The MathWorks, Inc.


%% Sort observations in ascending order. 
% Load the sample dataset array, |hospital|. Sort the observations by the
% values in |Age|, in ascending order. 
load hospital
dsAgeUp = sortrows(hospital,'Age');
dsAgeUp(1:10,{'LastName','Age'}) 

%%
% The youngest patients are age 25.  

%% Sort observations in descending order. 
% Sort the observations by |Age| in descending order. 
dsAgeDown = sortrows(hospital,'Age','descend');
dsAgeDown(1:10,{'LastName','Age'}) 

%%
% The oldest patients are age 50.  

%% Sort observations by the values of two variables. 
% Sort the observations in |hospital| by |Age|, and then by |LastName|. 
dsName = sortrows(hospital,{'Age','LastName'});
dsName(1:10,{'LastName','Age'}) 

%%
% Now the names are sorted alphabetically within increasing age groups.  

%% Sort observations in mixed order. 
% Sort the observations in |hospital| by |Age| in an increasing order, and
% then by |Weight| in a decreasing order. 
dsWeight = sortrows(hospital,{'Age','Weight'},{'ascend','descend'});
dsWeight(1:10,{'LastName','Age','Weight'}) 

%%
% This shows that the maximum weight among patients that are age 25 is 189 lbs.  

%% Sort observations by observation name. 
% Sort the observations in |hospital| by the observation names. 
dsObs = sortrows(hospital,'obsnames');
dsObs(1:10,{'LastName','Age'}) 

%%
% The observations are sorted by observation name in ascending alphabetical
% order.