Find the most value of a vector or matrix in MATLAB

In MATLAB, we often need to find the maximum value for a vector or matrix. This article mainly explains how to solve the most value problem of vector or matrix in MATLAB.

1. Solve the maximum and minimum values ​​of the vector

In MATLAB, the functions that want to solve the maximum and minimum values ​​of the data are the max and min functions, and these two functions are basically the same in usage.

There are two forms of how to find the most valued function of a vector:

(1)Y=max(X)和Y=max(X)

This way of calling is to find the maximum and minimum values ​​in the vector X, and assign this value to the vector Y. If there is a complex number in X, then the complex number element will be compared with its modulus length.

For example, let's take the following example:

X=[23,34,15,36,34,19,32,97,8,41];
Y1=max(X)
Y2=min(X)

The result is displayed as:

Y1 =
    97
Y2 =
     8

It can be seen from the results of the above formula that using this method can directly assign the maximum value to the desired variable.

(2)[Y,N]=max(X)和[Y,N]=min(X)

This calling method is to assign the most value of X in the vector to y, and assign the serial number of the most value to N.

We use this method to find the most value for the above vector:

X=[23,34,15,36,34,19,32,97,8,41];
[Y1,N1]=max(X)
[Y2,N2]=min(X)

The result looks like this:

Y1 =
    97
N1 =
     8
Y2 =
     8
N2 =
     9

2. Find the maximum and minimum values ​​of the matrix

(1) max(A) sum min(A)

The result of this matrix calling method is a row vector, and the i-th element in the row vector represents the largest value in the i-th column of the matrix.

For example:

X=[2,3,5,8;4,9,12,5;7,8,11,14;6,7,9,2];
Y=max(X)

The result looks like this:

Y =
     7     9    12    14

If the matrix contains complex numbers, its modulus length will be used for comparison. If the maximum value in the column is the modulus length of the complex number, then all elements of the row vector returned by the entire matrix will be displayed in the form of complex numbers. For example:

X=[2,3+7i,5,8;4,9,12+4i,5;7,8+9i,11+10i,14;6,7,9,2];
Y=max(X)

The running results are as follows:

Y =
   7.0000 + 0.0000i   8.0000 + 9.0000i  11.0000 +10.0000i  14.0000 + 0.0000i

(2) [Y,N]=max(X) or [Y,N]=min(X)

Return the row vector Y and N, the Y vector records the maximum value of each column of X, and the N vector represents the row number of the maximum value of each column.

X=[2,3,5,8;4,9,12,5;7,8,11,14;6,7,9,2];
[Y,N]=max(X)

The running results are as follows:

Y =
     7     9    12    14
N =
     3     2     2     3

(3) max(X,[],dim) and min(X,[],dim)

The value of dim in the above formula can be 1 or 2. If dim is 1, the function is the same as max(A); when dim is 2, the result returns a column vector, where the i-th element is the maximum value of the matrix.

For example, when the value of dim is 1:

X=[2,3,5,8;4,9,12,5;7,8,11,14;6,7,9,2];
[Y,N]=max(X,[],1)

The result of the operation is as follows:

Y =
     7     9    12    14
N =
     3     2     2     3

When the value of dim is 2:

X=[2,3,5,8;4,9,12,5;7,8,11,14;6,7,9,2];
[Y,N]=max(X,[],2)

The result looks like this:

Y =
     8
    12
    14
     9
N =
     4
     3
     4
     3

3. Examples

Here's a simple example, such as calculating the highest and lowest scores of candidates in a class. For example:

 For example, our code to calculate the maximum value of all grades looks like this:

X=xlsread('成绩.xlsx','sheet1','B2:B35');
Y=max(X)

The result looks like this:

Y =
    98

From the results, we can see that using the max() function can directly solve the maximum value relatively easily.

Guess you like

Origin blog.csdn.net/qq_54186956/article/details/126686034