You can sort a matrix by the size of its first column in MATLAB with the sortrows
function. Here is an example:
main.m60 chars3 lines
This will sort the matrix A
by the first column, so the output will be:
main.m56 chars5 lines
In this example, the first column is [5; 1; 9; 2]
, so the matrix is sorted based on these values.
Note that if you want to sort by the size (number of elements) of the first column instead of its values, you can use the size
function to get the size of the first column, and then sort based on that. Here's an example:
main.m87 chars4 lines
This will sort the matrix A
by the size of its first column, so the output will be:
main.m56 chars5 lines
In this example, the first column has size 4, so the last row comes first, followed by the first row which has size 3, etc.
gistlibby LogSnag