Main Content

sortrows

Sort rows of DataMatrix object in ascending or descending order

Description

DMObjNew = sortrows(DMObj1) sorts the rows in DMObj1 in the ascending order based on the elements in the first column. For any rows that have equal elements in a column, sorting is based on the column immediately to the right.

example

DMObjNew = sortrows(DMObj1,Column) sorts the rows in DMObj1 in the ascending order based on the elements in the specified column. Any rows that have equal elements in the specified column are sorted based on the elements in the next specified column.

example

DMObjNew = sortrows(DMObj1,RowName) sorts the rows in DMObj1 in the ascending order according to the row names.

example

DMObjNew = sortrows(___,Mode) specifies whether to sort the rows in the ascending or descending order Mode for any of the previous syntaxes.

example

[DMObjNew,Indices] = sortrows(DMObj1,___) also returns Indices, an index vector that links DMObj1 to DMObjNew. In other words, DMObjNew = DMObj1(idx,:).

example

Examples

collapse all

Load the MAT-file, provided with the software, that contains yeast data. This MAT-file includes three variables: yeastvalues, a 614-by-7 matrix of gene expression data, genes, a cell array of 614 GenBank® accession numbers for labeling the rows in yeastvalues, and times, a 1-by-7 vector of time values for labeling the columns in yeastvalues.

load filteredyeastdata

Create variables to contain a subset of the data, specifically the first five rows and first four columns of the yeastvalues matrix, the genes cell array, and the times vector.

yeastvalues2 = yeastvalues(1:5,1:4);
genes2 = genes(1:5,:);
times2 = times(1:4);

Create a DataMatrix object.

dmo = bioma.data.DataMatrix(yeastvalues2,genes2,times2)
dmo = 

                  0       9.5     11.5      13.5  
    SS DNA     -0.131    1.699    -0.026     0.365
    YAL003W     0.305    0.146    -0.129    -0.444
    YAL012W     0.157    0.175     0.467    -0.379
    YAL026C     0.246    0.796     0.384     0.981
    YAL034C    -0.235    0.487    -0.184    -0.669

Sort the rows in the ascending order based on Column 1.

dmo2 = sortrows(dmo,1)
dmo2 = 

                  0       9.5     11.5      13.5  
    YAL034C    -0.235    0.487    -0.184    -0.669
    SS DNA     -0.131    1.699    -0.026     0.365
    YAL012W     0.157    0.175     0.467    -0.379
    YAL026C     0.246    0.796     0.384     0.981
    YAL003W     0.305    0.146    -0.129    -0.444

Sort the rows in the descending order based on Column 1.

dmo3 = sortrows(dmo,1,"Descend")
dmo3 = 

                  0       9.5     11.5      13.5  
    YAL003W     0.305    0.146    -0.129    -0.444
    YAL026C     0.246    0.796     0.384     0.981
    YAL012W     0.157    0.175     0.467    -0.379
    SS DNA     -0.131    1.699    -0.026     0.365
    YAL034C    -0.235    0.487    -0.184    -0.669

Return an index vector that links the old object to the new object.

[dmoNew,Indices] = sortrows(dmo,1,"Descend")
dmoNew = 

                  0       9.5     11.5      13.5  
    YAL003W     0.305    0.146    -0.129    -0.444
    YAL026C     0.246    0.796     0.384     0.981
    YAL012W     0.157    0.175     0.467    -0.379
    SS DNA     -0.131    1.699    -0.026     0.365
    YAL034C    -0.235    0.487    -0.184    -0.669
Indices = 5×1

     2
     4
     3
     1
     5

In other words, you can use the index vector to recreate the new object from the old object.

dmoNew2 = dmo(Indices,:)
dmoNew2 = 

                  0       9.5     11.5      13.5  
    YAL003W     0.305    0.146    -0.129    -0.444
    YAL026C     0.246    0.796     0.384     0.981
    YAL012W     0.157    0.175     0.467    -0.379
    SS DNA     -0.131    1.699    -0.026     0.365
    YAL034C    -0.235    0.487    -0.184    -0.669

Input Arguments

collapse all

DataMatrix, specified as a bioma.data.DataMatrix object.

Column information, specified as a positive integer, vector of positive integers, character vector, string scalar, string vector or cell array of character vectors specifying column names, or logical vector.

Data Types: double | logical | char | string | cell

Row name, specified as a character vector or string scalar.

Data Types: char | string

Row sort order, specified as "ascend" or "descend".

Data Types: char | string

Output Arguments

collapse all

DataMatrix after sorting the columns, returned as a bioma.data.DataMatrix object.

Index vector that links DMObj1 to DMObjNew, returned as a numeric vector.

Version History

Introduced in R2008b