MatLabのソート関数-sort



Matlabs Sort Function Sort



The calling format of the sort function: Sort(X) Function: Returns a new vector sorted in ascending order of the elements in vector X. [Y, I] = sort(A, dim, mode) Function: Reorder each column or row of matrix A, I record the position of the element in Y before sorting A, where dim indicates whether to read column A or row Sort. If dim=1, sort by column if dim=2, sort by row. Mode is the sorting method, the value 'ascend' is ascending, and 'descend' is descending.

B = sort(A) % Rearranges the elements in A along the direction of the different dimensions of the input parameter A, from small to large. A can be a string, a real, or a complex array of cells. For the identical elements in A, they are arranged in a sequence according to their position in A if A is plural, they are arranged from small to large according to the amplitude of the elements. If there are plural elements with the same amplitude, press again. They are arranged in the range of the interval [-π , π ] from small to large if the elements in A are NaN, they are arranged to the end. If A is a vector, it returns a vector from small to large. If A is a two-dimensional matrix, it is arranged in the direction of the column. If A is a multi-dimensional array, sort(A) takes the element image vector along the first non-unit set. Treat it the same way. B=sort(A,dim) % Rearranges the elements in A along the dimension dim in the matrix A. [B, IX] = sort(A) % where IX is an array of size equal to size(A), each of which is the permutation position token corresponding to the element of the column vector in A. When sorting a vector (one-dimensional) in Matlab, you can use sort(A), where A is the vector to be sorted. If it is only used to sort A, then use sort(A) directly, if it is sorted. Need to keep the original index can use the return value, that is, [B, ind] = sort (A), after calculation, B is A sorted vector, A remains unchanged, ind is B, each item corresponds to A middle item index of. Sorting is performed in ascending order. In Matlab, access the elements in the matrix, one-dimensional accesses the first element of vector A with A(1) (subscript starts with 1) two-dimensional accesses the first line of A with A(1,2), The elements of the second column. Since the result of the sort function is sorted by Ascending order, to convert to descending order, first generate an n-dimensional unit matrix with X=eye(n), and then rotate it to the next diagonal with X=rot90(X). The unit matrix of the line, then multiply the original matrix by X. If you want to talk about A reverse order, use the following steps: X=eye(size(A)) X=rot90(X) A=A*X The above descending method is not good. Simple and easy to use: A=-sortrows(-A', the first few lines) A=A' Suppose a is a 2*n matrix, that is, two rows. b=a(1,:) [c,pos]=sort(b) %pos is the sorted subscript, c is the sort result of the first line a(2,:)=a(2,pos) % second line corresponds to the subscript sorted by the first line a(1,:)=c % first line result is reassigned to the first line of a The following matrix for m*n is sorted by the first line [ b, pos ] = sort( a( 1, : ) ) a = a( :, pos ) X=magic(5) X = 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 >> [a,b]=sort(X,2) a = 1 8 15 17 24 5 7 14 16 23 4 6 13 20 22 3 10 12 19 21 2 9 11 18 25 b = 3 4 5 1 2 2 3 4 5 1 1 2 3 4 5 5 1 2 3 4 4 5 1 2 3 Explanation of the results: a is the new matrix in which the original matrix x is arranged in rows, and each row is rearranged from small to large. b tells you the details of the rearrangement, that is, what changes have been made. For example, the first line of b shows 3 4 5 1 2, then the 3 4 5 1 2 elements of the first line of the original matrix X are taken out, and sequentially arranged to become the first line of the a matrix. Sort(X,2) and sort(X,1) respectively mean the following example: A = [ 3 3 5 0 4 2 ] Sort(A,1) % vertical alignment ans = 0 3 2 3 4 5 Sort(A,2) % horizontally aligned ans = 3 3 5 0 2 4 >>A = [-1.9, -0.2, 3.1415926, 5.6, 7.0, 2.4+3.6i] >>[B1,INDEX] = sort(A) The calculation result is: B1 = Columns 1 through 4 -0.2000 -1.9000 3.1416 2.4000 + 3.6000i Columns 5 through 6 5.6000 7.0000 INDEX = 2 1 3 6 4 5 It can be seen that if A is in one-to-one correspondence with another matrix C (char or cell), if [A1] is sorted by [B1, INDEX] = sort(A), the index INDEX can be obtained, and then each of B1 can be queried. Which element corresponds to C.

転載:https://blog.51cto.com/cathyyoung/1540163