Solving the determinant of the square matrix in MATLAB, solving the cumulative sum and cumulative product of the matrix, sorting the matrix, the rank and trace of the matrix, and solving the eigenvalue and eigenvector of the matrix

Table of contents

1. Calculation of determinant of square matrix

2. Accumulated sum and cumulative product

(1) cumulative sum

(2) cumulative product

3. Sort the data

4. Find the rank of the matrix

5. The trace of the matrix

6. Calculate the eigenvalues ​​and eigenvectors of the matrix

1. Calculation of determinant of square matrix

In linear algebra, to evaluate a square matrix, it needs to be converted into a determinant first. MATLAB provides the det function for evaluating the determinant of a square matrix, and finally calculates the converted determinant value.

For example:

A=[3,4,8;5,1,9;10,12,4];
B=det(A)

The calculated result is 368.

It should be noted that the calculated matrix must be a square matrix, otherwise the program will report an error.

2. Accumulated sum and cumulative product

In data processing, it is often necessary to perform cumulative sums and cumulative products for all data. The following calculations are performed in MATLAB for cumulative sums and cumulative products.

(1) cumulative sum

In MATLAB, the cumulative sum of the i-th element refers to the cumulative sum of all elements from the first of all data to the end of the i-th element. Assuming that the cumulative sum of the i-th element is s_i, and the previous data is x_1,x_2,x_3,...,x_i, then the formula can be deduced as follows:

s_i=\sum_{1}^{i}x_i

MATLAB provides the comsum function to calculate the cumulative sum from the first element to the i-th element, and the calling format is as follows (where V represents a vector and A represents a matrix):

  • cumsum(V): Solve the cumulative sum of the vector V.
  • cumsum(A): If the original matrix is ​​a matrix with m rows and n columns, then the cumsum(A) function returns a matrix with m rows and n columns, and the element in row i and column j of the matrix calculates the j column of the original matrix The cumulative sum of elements from row 1 to row i.
  • cumsum(A,num): When num=1, the effect is the same as cumsum(A). If the original matrix is ​​a matrix with m rows and n columns, then the cumsum(A,1) function returns a matrix with m rows and n columns. The element in row i and column j of the matrix calculates the cumulative sum of elements in column j of the original matrix from the element in row 1 to the element in row i. When num=2, if the original matrix is ​​a matrix with m rows and n columns, then the cumsum(A,2) function returns a matrix with m rows and n columns, and the element in row i and column j of the matrix calculates the original The cumulative sum of the i-th row of the matrix from the 1st column element to the j-th column element.
  • cumsum(A(:)): Calculate the cumulative sum of all elements from the first element to the position of the element in the matrix. The calculation order is to accumulate from each row of the first column. When the first column is all added, the second Columns are added...the last element is the cumulative sum of all elements in the matrix.

For example, to calculate the cumulative sum of the elements of a vector:

V=[2,3,4,5,6,4,6];
x=cumsum(V)

The running results are as follows:

x =
     2     5     9    14    20    24    30

Computes the cumulative sum of the elements in each column of a matrix from the element in row 1 to the element in the corresponding row.

A=[3,4,5,7;4,9,10,13;13,10,11,24];
x=cumsum(A)

The result after running is as follows:

x =
     3     4     5     7
     7    13    15    20
    20    23    26    44

Assuming that the original matrix is ​​a matrix with m rows and n columns, calculate the cumulative sum of each row of the matrix from the element in column 1 to the element in column j and the cumulative sum of each column of the matrix from row 1 to row i:

A=[3,4,5,7;4,9,10,13;13,10,11,24];
x=cumsum(A,1)
y=cumsum(A,2)

The running results are as follows:

x =

     3     4     5     7
     7    13    15    20
    20    23    26    44
y =

     3     7    12    19
     4    13    23    36
    13    23    34    58

Compute the cumulative sum of all elements in a matrix:

A=[32,4,16,20;31,24,15,19;10,12,18,22;41,22,20,26];
x=cumsum(A(:))

The result of the operation is as follows:

x =
    32
    63
    73
   114
   118
   142
   154
   176
   192
   207
   225
   245
   265
   284
   306
   332

As can be seen from the above results, the cumulative sum of the matrix is ​​332.

(2) cumulative product

In MATLAB, the cumulative product of the i-th element refers to the product of the cumulative multiplication of all elements from the first element of all data to the end of the i-th element. Assuming that the cumulative product of the i-th element is s_i, and the previous data is x_1,x_2,x_3,...x_i, then the formula can be deduced as follows:

s_i=\prod_{1}^{k}x_i

The cumprod function is provided in MATLAB. For calculating the cumulative product from the i-th element to the i-th element, the call format is as follows (where v represents a vector and A represents a matrix):

  • cumprod(V): Solve the cumulative product of the vector V.
  • cumprod(A): If the original matrix is ​​a matrix with m rows and n columns, then the cumprod(A) function returns a matrix with m rows and n columns, and the element in row i and column j of the matrix calculates the j column of the original matrix Cumulative product of elements from row 1 to row i.
  • cumprod(A,num): When num=1, the effect is the same as cumprod(A). If the original matrix is ​​a matrix with m rows and n columns, then the cumprod(A,1) function returns a matrix with m rows and n columns. The element in row i and column j of the matrix calculates the cumulative product from the element in row 1 to the element in row i in column j of the original matrix. When num=2, if the original matrix is ​​a matrix with m rows and n columns, then the cumprod(A,2) function returns a matrix with m rows and n columns, and the element in row i and column j of the matrix calculates the original The cumulative product of the i-th row of the matrix from the first column element to the j-th column element.
  • cumpord(A(:)): Calculate the cumulative product of all the elements from the first element to the position of the element in the matrix. The calculation order starts from the first column, and each row performs cumulative multiplication. After the first column is all multiplied, Multiply the second column again...the last element is the cumulative product of all the elements in the matrix.

For example, to calculate the cumulative product of the elements of a vector:

V=[2,1,3,4,5,2,3];
x=cumprod(V)

The running results are as follows:

x =
     2     2     6    24   120   240   720

Compute the cumulative product of the elements in each column of a matrix from the element in row 1 to the corresponding element:

A=[3,4,5,6;6,2,1,2;4,3,2,1];
x=cumprod(A)

The running results are as follows:

x =
     3     4     5     6
    18     8     5    12
    72    24    10    12

Assuming that the original matrix is ​​a matrix with m rows and n columns, calculate the cumulative product of each row of the matrix from the element in the first column to the element in the jth column and calculate the cumulative product of each column of the matrix from the element in the first row to the end of the element in the i row :

A=[3,4,5,7;4,9,10,13;13,10,11,24];
x=cumprod(A,1)
y=cumprod(A,2)

The running results are as follows:

x =

           3           4           5           7
          12          36          50          91
         156         360         550        2184
y =

           3          12          60         420
           4          36         360        4680
          13         130        1430       34320

Compute the cumulative product of all elements in a matrix:

A=[2,3,6;8,5,2;9,11,12];
x=cumprod(A(:))

The running results are as follows:

x =
           2
          16
         144
         432
        2160
       23760
      142560
      285120
     3421440

In the above operation result, the last element 3421440 is the product of all elements in the matrix.

3. Sort the data

When processing data, it is often necessary to sort the data. Therefore, when writing a program, the serial number sorts the data. For example, there are ten classic sorting algorithms in the data structure. Also in MATLAB, the sort function is also provided to sort the data.

The sort function returns a new matrix, which is sorted from small to large by default.

  • sort(V): Sort the vector V, and the returned result is a vector that has been sorted from small to large.
  • sort(A): sorts the elements of each column of the matrix, and returns a matrix in which the elements of each column are sorted from small to large.
  • sort(A,num): When num=1, the effect is the same as sort(A), and returns a good matrix in which the elements of each column are sorted from small to large; when num=2, for each matrix One row is sorted, and the returned result is a matrix in which the elements of each row are sorted from small to large.
  • sort(A(:)): Returns a column vector that sorts all elements of the matrix from small to large.

For example, the following uses the sort function to sort a vector:

V=[34,23,6,41,65,32,7,53];
V=sort(V)

The result after the operation is as follows:

V =
     6     7    23    32    34    41    53    65

It can be seen that after the vector is sorted by the sort function, the vector V becomes a vector in which each element is arranged in order from small to large.

For another example, use the sort function to sort each column of the matrix:

A=[2,5,3;6,10,7;4,9,11;12,4,3];
sort(A)

The running results are as follows:

ans =
     2     4     3
     4     5     3
     6     9     7
    12    10    11

It can be seen from the above operation results that the sort function sorts the elements of each column of the matrix from small to large.

For another example, use the sort function to sort each row and column of the matrix separately:

A=[2,5,3;6,10,7;4,9,11;12,4,3];
x=sort(A,1)
y=sort(A,2)

The running results are as follows:

x =

     2     4     3
     4     5     3
     6     9     7
    12    10    11
y =

     2     3     5
     6     7    10
     4     9    11
     3     4    12

It can be seen from the above calculation results that when the num of sort(A, num) is 1 or 2, the columns and rows of the matrix are sorted respectively.

Use the sort function to sort all the elements of the matrix:

A=[2,3,6;8,5,2;9,11,12];
sort(A(:))

The running results are as follows:

ans =
     2
     2
     3
     5
     6
     8
     9
    11
    12

As can be seen from the running results, sort(A(:)) returns a column vector, which can sort all the elements of the matrix.

4. Find the rank of the matrix

The rank of a matrix is ​​an important tool for linear correlation of matrices and the solution of linear equations. The definition is as follows: The rank of a matrix is ​​a concept in linear algebra. In linear algebra, the column rank of a matrix A is the maximum number of linearly independent columns, and the row rank is the maximum number of linearly independent rows of the matrix. That is, if the matrix is ​​regarded as row vectors or column vectors, the rank is the rank of these row vectors and column vectors, that is, the number of vectors contained in the maximum irrelevant group. (This paragraph defines the rank of the matrix from Baidu Encyclopedia )

In our actual operation process, the general operation method is to convert the matrix into a row-echelon matrix, and calculate the number of rows that are not all 0 in the entire row, and this number is the rank of the matrix.

In MATLAB, the rank function is provided for calculating the rank of the matrix, for example:

A=[1,2,3;2,4,6;8,4,7];
rA=rank(A)
B=[23,6,5;6,3,11;7,13,12];
rB=rank(B)

The result of the operation is as follows:

rA =
     2
rB =
     3

The above calculation results show that the rank of matrix A is 2, the rank of matrix B is 3, and both A and B are third-order matrices. Through the calculation of the rank of the matrix, it can be obtained that A is a full-rank matrix, and B is a Not satisfied rank matrix.

5. The trace of the matrix

For a square matrix A, the sum of all diagonals on the diagonal of the square matrix is ​​called the trace of the matrix, which is tr(A). Obviously, the calculation method of the trace of the matrix is:

A_{11}+A_{22}+A_{33}+...+A_{nn}=tr(A)

 In MATLAB, the trace function of the calculation matrix is ​​provided. For example:

A=[4,5,3;7,5,3;9,11,3];
B=trace(A)

The result of the operation is as follows:

B =
    12

6. Calculate the eigenvalues ​​and eigenvectors of the matrix

The concept of eigenvalues ​​was introduced by the French scientist Laplace in the 19th century when he studied celestial mechanics and earth mechanics. In practical applications, the eigenvalues ​​of matrices are widely used.

Suppose A is an n-order matrix, if the number \lambdaand n-dimensional non-zero vector x satisfy the following conditions:

Ax=\lambda x

 Then \lambdait is the eigenvalue of the square matrix A, and the non-zero vector x is called the \lambdaeigenvector of the corresponding eigenvalue of the square matrix A.

The above formula can also be written in the form:

(A-\lambda E)x=0

The conditions for the above formula to have a non-zero solution are:

|A-\lambda E|=0

It can also be written as follows: 

\begin{pmatrix} A_{11}-\lambda & A_{12} & \cdots & A_{1n}\\ A_{21} & A_{22}-\lambda & \cdots & A_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ A_{n1} & A_{n2} & \cdots & A_{nn}-\lambda \end{pmatrix}=0

 The eig function is provided in MATLAB to calculate the eigenvectors and eigenvalues ​​of the matrix, and the calling format is as follows:

  • e=eig(A): e is a column vector and calculates all the eigenvalues ​​in the matrix A.
  • [V,R]=eig(A): R is a diagonal matrix composed of the eigenvalues ​​of the matrix A, the elements on the diagonal matrix R are all the eigenvalues ​​of the matrix A, and V is the corresponding eigenvector of each column of A.

Use the eig function of MATLAB to calculate the eigenvalue of the matrix, for example:

A=[0,1,1;1,0,1;1,1,0];
B=[4,5,5;3,6,7;8,9,4];
e1=eig(A)
e2=eig(B)

The result looks like this:

e1 =
   -1.0000
   -1.0000
    2.0000

e2 =
   17.0296
    0.6537
   -3.6832

Another example:

A=[12,16,8;9,23,11;24,18,4]
[V,D]=eig(A)

 The running results are as follows:

V =

   -0.4994   -0.4102   -0.1379
   -0.6110    0.5782   -0.3173
   -0.6143   -0.7053    0.9382
D =

   41.4154         0         0
         0    3.1994         0
         0         0   -5.6149

From the above content, we can see that the eigenvalues ​​of matrix A are 41.4154, 3.1994, -5.6149 respectively, and matrix V is the eigenvector of each column of matrix A.

Guess you like

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