Matlab: Sort 2D array with respect to one column

sort function in Matlab, sorts matrix columns independently.
But,
B = sortrows(A, column) sorts the matrix based on the columns specified in the vector column. 

Example:


>> A = [2 10; -1 20; 5 40]

A =

     2    10
    -1    50
     5    15

>> sortrows(A, 1)

ans =

    -1    50
     2    10
     5    15

>> sortrows(A, 2)

ans =

     2    10
    -1    50
     5    15



No comments:

Post a Comment

Python contextlib for Timing Python code

If you've ever found yourself needing to measure the execution time of specific portions of your Python code, the `contextlib` module o...