主要内容

种类

Sort elements of symbolic arrays

描述

example

Y=排序(X)种类s the elements ofXin ascending lexicographical order.

  • 如果X是一个向量,然后排序(x)分类矢量元素X

  • 如果X是矩阵,然后排序(x)treats the columns ofX作为向量和每个列分别独立分类。

  • 如果X是一个多维数组,然后排序(x)operates along the first array dimension whose size does not equal 1, treating the elements as vectors.

example

Y=排序(X,dim)种类s the elements ofX沿尺寸dim。例如,如果Xis a two-dimensional matrix, then种类(X,1)分类每一列的元素X, 和排序(x,2)分类每一行的元素。

example

Y=排序(___,方向)返回分类元素X按照指定的顺序方向使用任何先前的语法。'ascend'indicates ascending order (the default), and'descend'indicates descending order.

example

[Y,I] = sort(___)还返回以前任何语法的索引向量集合。I大小与Xand describes the arrangement of the elements ofXintoY沿着排序的维度。例如,如果Xis anm-经过-nmatrix and you sort the elements of each column (dim = 1), then each column ofI是分类列的索引向量X,这样

forj = 1:n Y(:,j) = X(I(:,j),j);end

例子

collapse all

Create a symbolic row vector and sort its elements in ascending order.

symsabcde排序([[7 E 1 C 5 D A B])
ans =
                      
                       
                        
                         
                          
                           (
                          
                           
                            
                             
                              
                               1
                             
                            
                            
                             
                              
                               5
                             
                            
                            
                             
                              
                               7
                             
                            
                            
                             
                              
                               a
                             
                            
                            
                             
                              
                               b
                             
                            
                            
                             
                              
                               c
                             
                            
                            
                             
                              
                               d
                             
                            
                            
                             
                              
                               e
                             
                            
                           
                          
                          
                           )
                         
                        
                        
                         [sym(1), sym(5), sym(7), a, b, c, d, e]
                       
                      

When sorting the elements of a matrix,种类can work along the columns or rows of that matrix.

创建一个符号矩阵。

X= magic(3)*sym('a')
X=

( 8 a a 6 a 3 a 5 a 7 a 4 a 9 a 2 a ) [8*a,a,6*a;3*a,5*a,7*a;4*a,9*a,2*a]

Sort the matrixX。By default, the种类command sorts the elements of each column.

Y = sort(X)
Y =

( 3 a a 2 a 4 a 5 a 6 a 8 a 9 a 7 a ) [3*a,a,2*a;4*a,5*a,6*a;8*a,9*a,7*a]

要对每一行的元素进行排序,请设置dim选项2

Y = sort(X,2)
Y =

( a 6 a 8 a 3 a 5 a 7 a 2 a 4 a 9 a ) [a, 6*a, 8*a; 3*a, 5*a, 7*a; 2*a, 4*a, 9*a]

创建一个符号矩阵。

X= magic(3)*sym('a')
X=

( 8 a a 6 a 3 a 5 a 7 a 4 a 9 a 2 a ) [8*a,a,6*a;3*a,5*a,7*a;4*a,9*a,2*a]

Sort the elements of each row in descending order.

Y = sort(X,2,'descend')
Y =

( 8 a 6 a a 7 a 5 a 3 a 9 a 4 a 2 a ) [8*a,6*a,a;7*a,5*a,3*a;9*a,4*a,2*a]

找到矩阵的每个元素的索引Y在原始矩阵中X, call种类with two output arguments.

Create a symbolic matrixX

X= magic(3)*sym('a')
X=

( 8 a a 6 a 3 a 5 a 7 a 4 a 9 a 2 a ) [8*a,a,6*a;3*a,5*a,7*a;4*a,9*a,2*a]

Sort each column ofXand return the indices of the sorted elements inI。Each column ofIcontains the presorted positions of entries inY

[Y,我]=琼t(X)
Y =

( 3 a a 2 a 4 a 5 a 6 a 8 a 9 a 7 a ) [3*a,a,2*a;4*a,5*a,6*a;8*a,9*a,7*a]

I =3×32 1 3 3 2 1 1 3 2

排序符号向量Xthat contains real and complex numbers. WhenX包含符号真实和复杂数,排序(x)returns the sorted real numbers, followed by the sorted complex numbers based on their real parts.

X=排序(sym([2 -1/2 3+4i 5i 4+3i]))
X=

( - 1 2 2 5 i 3 + 4 i 4 + 3 i ) [-sym(1/2), sym(2), sym('5i'), sym(3) + 4i, sym(4) + 3i]

Create a 2-by-2-by-2 symbolic array that contains symbolic numbers, variables, and functions.

symsxyf(x);x(:,:,1)= [y 1;1/3 x];x(:,::,2)= [x -2;1/4 F(x)];X
X(:,:,1) =

( y 1 1 3 x ) [y,Sym(1);sym(1/3), x]

x(:,::,2)=

( x - 2 1 4 f ( x ) ) [x,-sym(2);sym(1/4), f(x)]

Sort its elements in ascending order along the third dimension.

y =排序(x,3)
Y(:,:,1) =

( x - 2 1 4 x ) [x,-sym(2);sym(1/4),x]

Y(:,:,2) =

( y 1 1 3 f ( x ) ) [y,Sym(1);SYM(1/3),F(x)]

利用X(:), the column representation ofX, to sort all of the elements ofX

Y = sort(X(:))
Y =

( - 2 1 4 1 3 1 x x y f ( x ) ) [-sym(2);SYM(1/4);SYM(1/3);sym(1);X;X;y;F(x)]

Input Arguments

collapse all

输入阵列,指定为符号向量,矩阵或多维数组。种类使用以下规则:

  • 如果X仅包含合理的符号实数,然后排序(x)种类s the elements numerically.

  • 如果Xcontains only symbolic complex numbers with rational real and imaginary parts, then排序(x)首先按照它们的真实部位对元素进行分类,然后通过他们的虚构部分打破联系。

  • 如果X仅包含符号变量,然后排序(x)种类s the elements alphabetically.

  • 如果X包含符号数字(带有理性零件)和变量的混合物,然后排序(x)returns the following sequence: sorted real numbers, sorted complex numbers, and sorted variables.

  • 如果X包含符号数字,表达式和函数,比较和分类元素可能在计算上很复杂。所以,种类使用内部排序规则来优化其性能。

沿着操作的维度,指定为正整数标量。如果未指定值,则默认值是第一个数组维度,其大小不等于1。

Consider a two-dimensional input arrayX:

  • 种类(X,1)分类列中的元素X

    沿列的2 x-3矩阵排序。

  • 排序(x,2)分类成行的元素X

    Sorting of a 2-by-3 matrix along the rows.

种类returnsXifdim大于ndims(X)

分类方向,指定为'ascend'或者'descend'

Output Arguments

collapse all

排序的数组,作为符号向量,矩阵或多维数组返回。Y是相同的大小和类型X

Sort index, returned as a symbolic vector, matrix, or multidimensional array.I大小与X。The index vectors are oriented along the same dimension that种类操作。例如,如果Xis a 2-by-3 matrix, then[y,i] =排序(x,2)种类s the elements in each row ofX。输出I是包含每一行的预分数位置的1 x-3行索引向量的集合Y

提示

  • 打电话种类对于不是符号对象的数字数组®种类function.

  • The种类function sorts symbolic complex numbers differently from MATLAB floating-point complex numbers. For symbolic inputX包含复数数字,排序(x)种类s the complex numbers first by their real parts, then by their imaginary parts to break ties. For floating-point inputX, by default,排序(x)种类s complex numbers by their magnitude, followed by their phase angles in the interval (−π, π] to break ties.

在R2006a之前引入