Download LyME User Manual
Transcript
LME Reference — arrays
261
The optional second output argument gives the permutation array
which transforms A into the sorted array. It can be used to reorder
elements in another array or to sort the rows of a matrix with respect
to one of its columns, as shown in the last example below. Order of
consecutive identical elements is preserved.
If a second numeric argument dim is provided, the sort is performed
along dimension dim (columns if dim is 1, rows if 2, etc.)
An additional argument can specify the ordering direction. It
must be the string ’ascending’ (or ’a’) for ascending order, or
’descending’ (or ’d’) for descending order. In both cases, NaNs are
moved to the end.
sort(list) sorts the elements of a list, which must be strings. Cell
arrays are sorted like lists, not column-wise like numeric arrays. The
second output argument is a row vector. The direction can be specified
with a second input argument.
Examples
sort([3,6,2,3,9,1,2])
1 2 2 3 3 6 9
sort([2,5,3;nan,4,2;6,1,1])
2
1
1
6
4
2
nan 5
3
sort([2,5,3;nan,4,2;6,1,1], ’d’)
6
5
3
2
4
2
nan 1
1
sort({’def’, ’abcd’, ’abc’})
{’abc’, ’abcd’, ’def’}
To sort the rows of an array after the first column, one can obtain the
permutation vector by sorting the first column, and use it as subscripts
on the array rows:
M = [2,4; 5,1; 3,9; 4,0]
2 4
5 1
3 9
4 0
(Ms, ix) = sort(M(:,1));
M(ix,:)
2 4
3 9
4 0
5 1
Related documents