OKPEDIA MATLAB EN VETTORI

How to sort a vector in Matlab and Octave

To sort a vector in ascending or descending order in Matlab and Octave, use sort() function

sort(v, [ascend/descend])

The first parameter v is a vector (array variable).

The second parameter is the ascending or descending order

This function sorts the elements in ascending or descending order.

Note. The second parameter is optional. If the second parameter is not specified, the sort () function sorts the elements in ascending order by default.

Examples

Example 1

Create a numeric vector

>> v=[5, 3, 7, 1, 4]

To sort the vector use sort() function

>> >> sort(v)
ans =
1 3 4 5 7

The result is a vector with the elements in ascending order.

Example 2

To sort the previous vector in descending order, use the sort () function with the 'descend' option

>> >> sort(v, 'descend')
ans =
7 5 4 3 1

The result is a vector with the elements sorted in descending order.

https://how.okpedia.org/en/matlab/how-to-sort-a-vector-in-matlab-and-octave


Report an error or share a suggestion to enhance this page


Arrays and Vectors


FacebookTwitterLinkedinLinkedin