This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

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.

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');
ans = 

               LastName           Age
    XUE-826    'JACKSON'          25 
    FZR-250    'HALL'             25 
    PUE-347    'YOUNG'            25 
    LIM-480    'HILL'             25 
    SCQ-914    'JAMES'            25 
    REV-997    'ALEXANDER'        25 
    XBR-291    'GARCIA'           27 
    VNL-702    'MOORE'            28 
    DTT-578    'WALKER'           28 
    XAX-646    'COOPER'           28 

The youngest patients are age 25.

Sort observations in descending order.

Sort the observations by Age in descending order.

dsAgeDown = sortrows(hospital,'Age','descend');
ans = 

               LastName          Age
    XBA-581    'ROBINSON'        50 
    DAU-529    'REED'            50 
    XLK-030    'BROWN'           49 
    FLJ-908    'STEWART'         49 
    GGU-691    'HUGHES'          49 
    MEZ-469    'GRIFFIN'         49 
    KOQ-996    'MARTIN'          48 
    BKD-785    'CLARK'           48 
    KKL-155    'ADAMS'           48 
    NSK-403    'RAMIREZ'         48 

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'});
ans = 

               LastName           Age
    REV-997    'ALEXANDER'        25 
    FZR-250    'HALL'             25 
    LIM-480    'HILL'             25 
    XUE-826    'JACKSON'          25 
    SCQ-914    'JAMES'            25 
    PUE-347    'YOUNG'            25 
    XBR-291    'GARCIA'           27 
    XAX-646    'COOPER'           28 
    QEQ-082    'COX'              28 
    NSU-424    'JENKINS'          28 

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'});
ans = 

               LastName           Age    Weight
    FZR-250    'HALL'             25     189   
    SCQ-914    'JAMES'            25     186   
    XUE-826    'JACKSON'          25     174   
    REV-997    'ALEXANDER'        25     171   
    LIM-480    'HILL'             25     138   
    PUE-347    'YOUNG'            25     114   
    XBR-291    'GARCIA'           27     131   
    NSU-424    'JENKINS'          28     189   
    VNL-702    'MOORE'            28     183   
    XAX-646    'COOPER'           28     127   

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');
ans = 

               LastName         Age
    AAX-056    'LEE'            44 
    AFB-271    'PEREZ'          44 
    AFK-336    'WRIGHT'         45 
    AGR-528    'SIMMONS'        45 
    ATA-945    'WILSON'         40 
    BEZ-311    'DIAZ'           45 
    BKD-785    'CLARK'          48 
    DAU-529    'REED'           50 
    DGC-290    'BUTLER'         38 
    DTT-578    'WALKER'         28 

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

See Also


Related Examples

More About

Was this topic helpful?