matlab basic function sort

The evolution together substantially sort function calculation -matlab

Find useful, welcome to discuss mutual learning together - Follow Me

sort

  • Sort the array elements

    grammar

  • B = sort (A) is not equal to 1 in accordance with the size of the first dimension of the array element A is sorted in ascending
    • If A is a vector, then the Sort (A) sorts the elements of the vector
    • If A is a matrix, then the Sort (A) of the column A as a vector, and each column to sort
    • If A is a multidimensional array, the Sort (A) is not equal to the first dimension of the array size along an operation, an element vector is considered.
  • B = sort(A,dim)
    • Back along the dimension dim sorted elements. For example, if A is a matrix, then the sort (A, 2) sorting the elements in each row
  • B = sort (___, direction) returns any preceding sequentially ordered syntax elements in the specified direction. Single string "ascend" for ascending (default), "descent" indicates descending.
  • [B, I] = sort ( ___) return to the previous set index vector of any syntax. I is the same size as A, a case is described in the ordering of elements arranged in the dimension B of A. For example, if A is a numeric vector, B = A (I). That is, the index returned after collating elements

    Examples

    Ascending order vector

  • Creating a row vector, according to its elements in ascending order
A = [9 0 -7 5 3 8 -10 4 2];
B = sort ()
B =
-10 7 0 2 3 4 5 8 9

Of the rows of the matrix in ascending order

A = [3 6 5; 7 -2 4; 1 0 -9]
A =

     3     6     5
     7    -2     4
     1     0    -9

B = sort(A,2)
B =

     3     5     6
    -2     4     7
    -9     0     1

Of columns in the matrix in descending order

A = [10 -12 4 8; 6 -9 8 0; 2 3 11 -2; 1 1 9 3]
A =

    10   -12     4     8
     6    -9     8     0
     2     3    11    -2
     1     1     9     3

B = sort(A,'descend')
B =

    10     3    11     8
     6     1     9     3
     2    -9     8     0
     1   -12     4    -2

Sort the array and index date

Create a datetime value in the array, in ascending order, from the earliest to the most recent calendar date calendar date

ds = {'2012-12-22';'2063-04-05';'1992-01-12'};
A = datetime(ds,'Format','yyyy-MM-dd')
A =

   2012-12-22
   2063-04-05
   1992-01-12

[B,I] = sort(A)
B =

   1992-01-12
   2012-12-22
   2063-04-05


I =

     3
     1
     2

B列出了排序后的日期,I包含了A对应的索引。使用索引数组I直接访问原始数组中已排序的元素

A(I)
ans =

   1992-01-12
   2012-12-22
   2063-04-05

3-D array sorting

Create an array of 2 × 2 × 2, and which elements are arranged in ascending order along the third dimension

A(:,:,1) = [2 3; 1 6];
A(:,:,2) = [-1 9; 0 12];
A
A(:,:,1) =

     2     3
     1     6


A(:,:,2) =

    -1     9
     0    12

B = sort(A,3)
B(:,:,1) =

    -1     3
     0     6


B(:,:,2) =

     2     9
     1    12

% 使用A(:),表示对A的所有元素进行排序

B = sort(A(:))
B =

    -1
     0
     1
     2
     3
     6
     9
    12

Input parameters

Output parameters

Guess you like

Origin www.cnblogs.com/cloud-ken/p/11259385.html