Thoroughly understand the usage of max and min functions in Matlab through examples (for maximum and minimum values)

Doing Matlab homework today requires you to find the maximum and minimum values, check some information, and summarize. The max and min functions are mainly used in the following ways.

max(A):

        If A is a row vector, that is, a 1 x M-dimensional vector, the maximum value of the row is returned.

E.g:

                  Define a row vector in the command line window of matlab:

                  A = [4 3 2 5 6 8];      1 x 6

                  Then use max(A), the result is: 8.

        If A is a column vector, that is, an M x 1-dimensional vector, the maximum value of this column is returned.

E.g:

                  Define a column vector in the command line window of matlab:

                  A = [4;3;2;5;6;8]      6 x 1

                  Then use max(A), the result is: 8.

If A is a matrix, that is, an M x N-dimensional matrix, the max(A) function compares each column of the matrix, that is, finds the maximum value of the data in the N columns, and returns a 1 x N-dimensional row vector , because 1 column seeks a maximum value, so N columns require N maximum values.

E.g:

                  Define a matrix in the command line window of matlab:

                  A = [4 6 2 3 5;6 8 2 9 1;12 23 18 2 7] --->3 x 5 (three rows and five columns, M=3, N=5)

                  Then use max(A), the result is: 12 23 18 9 7

                  as the picture shows:

Summary: You can remember a rule, that is: max(A) returns a row vector (returns a value that is a 1 x 1 row vector), and the ith element of the vector is A (A can be a row vector column vector and the maximum value on the i-th column of the matrix).

max(A,[],dim):

        This function is mainly used when A is a matrix. The full name of dim is dimension, which means dimension. dim can take 1 or 2.

       When 1 is taken, the meaning is the same as the result obtained by max(A) above. The maximum value of each column of the A matrix is ​​obtained, and a 1 x N-dimensional row vector is returned. The i-th row vector of this row vector is returned. value, which represents the maximum value of the i-th column of this matrix A.

    E.g:

                  Define a matrix in the command line window of matlab:

                  A = [4 6 2 3 5;6 8 2 9 1;12 23 18 2 7] --->3 x 5 (three rows and five columns, M=3, N=5)

                  Then use max(A,[],1), the result is: 12 23 18 9 7

                 as the picture shows:

       When dim is set to 2, the maximum value of each row of the A matrix is ​​obtained, and an Mx 1-dimensional column vector is returned. The ith value of this column vector represents the maximum value of the ith row of this matrix A. . Pay attention to the difference from when dim is equal to 1. It can be understood as follows: when dim is equal to 1, it is to find the maximum value of each column, and N columns will have N values; when dim is equal to 2, it is to find the maximum value of each row of the matrix, and M rows There will be M values.

E.g:

                  Define a matrix in the command line window of matlab:

                  A = [4 6 2 3 5;6 8 2 9 1;12 23 18 2 7] --->3 x 5 (three rows and five columns, M=3, N=5)

                  Then use max(A,[],2), the result is: 6; 9; 3

                 as the picture shows:

 What if we not only want to get the maximum or minimum value, we also want to get the position of the maximum or minimum value?

 [Y, Z] = max(A, [], dim);

        This function not only allows us to get the maximum or minimum value, we can also get the column or row where it is located.

        Y: What is obtained here is the set of returned maximum or minimum values, such as the above max(A,[],dim): when dim is equal to 1, the returned result is assigned to Y, and when dim is equal to 2, then The returned result is assigned to Y, that is, the maximum or minimum value that Y is used to receive.

        Z: If dim=1, what is obtained here is the number of rows in matrix A for each value in the Y result, that is, which row of matrix A the value in Y appears in.

        E.g:

                  Define a matrix in the command line window of matlab:

                  A = [4 6 2 3 5;6 8 2 9 1;12 23 18 2 7] --->3 x 5 (three rows and five columns, M=3, N=5)

                  as the picture shows:

                  Then use [Y,Z] = max(A,[],1)

                 The result is: Y = 12 23 18 9 7

                                Z =  3     3       3     2     3

              That is, for each maximum value in Y, the number of rows in the matrix where it is located is stored in Z.

              Z: If dim=2, what is obtained here is the column number of each value in the Y result in matrix A, that is, which column of matrix A the value in Y appears in.          

         E.g:

                  Define a matrix in the command line window of matlab:

                  A = [4 6 2 3 5;6 8 2 9 1;12 23 18 2 7] --->3 x 5 (three rows and five columns, M=3, N=5)

                  as the picture shows:

                  Then use [Y,Z] = max(A,[],2)

                 The result is: Y = 6
                                       9
                                       23

                                Z =  2
                                       4
                                       2

                 as the picture shows:

                 That is, for each maximum value in Y, the number of columns in the matrix where it is located is stored in Z.

Summary: When dim is equal to 1, the value in Y is the set of the maximum value of matrix A according to the column, and the row number of the maximum value in the matrix is ​​stored in Z; when dim is equal to 2, the value in Y is the matrix A according to the row. The set of maximum values, Z stores the column number of the maximum value in the matrix.

The usage of min is the same as above, just replace the keyword max with min.

Please indicate the source.

Guess you like

Origin blog.csdn.net/BaoITcore/article/details/123645614