当前位置:网站首页>From introduction to mastery of MATLAB (2)

From introduction to mastery of MATLAB (2)

2022-04-23 18:08:00 Kyoto Xiaobai


The notes come from B Stop video link, I recommend you to have a look

Topic two MATLAB Matrix processing

2.1 Special matrix

One is the general special matrix , The other is the special matrix for special subjects

  • General special matrix

    • zeros function : Produce all 0 matrix , Zero matrix
    • ones function : Produce all 1 matrix , Unitary matrix
    • eye function : The resulting diagonal is 1 Ground matrix . When the matrix is a square matrix , Get an identity matrix .
    • rand function : produce (0,1) Interval uniformly distributed random matrix
    • randn function :n representative normal, The meaning of normal distribution , The average production is 0, The variance of 1 Standard normal distribution random matrix

    The call formats of these functions are similar , With zero Function as an example to illustrate :

    • zeros(m): produce m×m The zero matrices of

    • zeros(m,n): produce m That's ok n Zero matrix of columns

    • zeros(size(A)): Resulting from a zero matrix of the same size

 Insert picture description here

  • Special matrices for specialized disciplines

    1. Magic matrix ——Magic Square

      • n The first order magic cube array consists of 1,2,3,……n2 common n2 An integer makes up , And every line 、 Each column and main 、 On the sub diagonal n The sum of the elements is equal
      • n The sum of the elements in each row and column of the first-order magic cube array is (1+2+3+……+n2)/n=(n+n3)/2
      • MATLAB function magic(n) Only one specific magic cube matrix is generated .

 Insert picture description here

  1. Vandermonde matrix

    For vector v=[v1,v2,……,vn], The general form of Vandermonde matrix is :

 Insert picture description here

stay MATLAB in , function vander(V) Generate a vector V Based on Vandermonde matrix .

 Insert picture description here

Vandermonde matrix is often used in error correction coding of various communication systems , Such as Reed-Solomon Coding is based on Vandermonde matrix .

  1. Hilbert matrix

n The general form of order Hilbert matrix is :

 Insert picture description here

H(i,j)=1/(i+j-1)

stay MATLAB in , Generate n The function of order Hilbert matrix is hilb(n).

 Insert picture description here

  1. Adjoint matrix

    A polynomial p(x) by anxn+an-1xn-1+……+a1x+a0, Then the adjoint matrix of the polynomial is :

 Insert picture description here

p(x) It's called a matrix A Characteristic polynomials of , equation p(x)=0 The root of is called A The eigenvalues of the .

MATLAB The function that generates the adjoint matrix in is compan(p), among p Is the coefficient vector of a polynomial , Higher power coefficients rank first , The lower power coefficient comes next . For example, generation x3-2x2-5x+6 The adjoint matrix of :

 Insert picture description here

  1. Pascal matrix

    According to the binomial theorem ,(x+y)n The expanded coefficient increases with n The increase of forms a triangular table , This triangle is called Yang Hui triangle . Each row of triangles here represents a different n Binomial coefficient of , Fill in the binomial coefficients on the left diagonal of the matrix in turn , Then extract the left n That's ok n The column element is n Order Pascal matrix .

 Insert picture description here

The elements of the first row and the first column of the Pascal matrix are 1, For elements in other positions, the left element of the element is added to the upper element , namely P(i,j)=P(i,j-1)+P(i-1,j), And P(i,1)=1,P(1,j)=1.

 Insert picture description here

2.2 Matrix transformation

Matrix change refers to the operation of a matrix , The result is also a matrix .

  • Diagonal matrix

    Diagonal matrix : Only matrices with non-zero elements on the diagonal .

    Quantity matrix : A diagonal matrix in which the elements on the diagonal are equal .

    Unit matrix : The elements on the diagonal are 1 Diagonal matrix of .

    1. Extract the diagonal elements of the matrix

      • diag(A): Extract matrix A Main diagonal element , Produces a column vector .

      • diag(A,K): Extract matrix A The first K Diagonal elements , Generate a column vector .

        Parallel to the main diagonal , Up for the first 1,2,……n strip , Down is the second -1,-2,……-n strip .

    2. Construct diagonal matrix

      • diag(V): With vectors V Main diagonal element , Generate a diagonal matrix .
      • diag(V,K): With vectors V For the first time k Diagonal elements , Generate a diagonal matrix .
  • Triangles

    1. Upper triangular matrix

      A matrix whose elements below the diagonal of the matrix are all zero

      • triu(A): Extract matrix A The main diagonal of and above .

      • triu(A,k): Extract matrix A Of the k Elements with diagonal lines and above .

 Insert picture description here

  1. Lower triangular array

    A matrix whose elements above the diagonal are all zero

    stay MATLAB in , Extract matrix A The function of the lower triangular matrix of is tril, Its usage and triu The function is exactly the same .

  • The transpose of the matrix

    Matrix row column exchange .

    • The transpose operator is a decimal point followed by a single quotation mark (.').

    • Conjugate transpose , The operator is a single quotation mark (’), On the basis of transpose, it also takes the complex conjugate of each number .

 Insert picture description here

 If the elements of the matrix are real numbers , So the result of transpose and conjugate transpose is the same .
  • The rotation of the matrix

    • rot90(A,K): The matrix A Turn counter clockwise 90° Of k times , When k by 1 It can be omitted .

 Insert picture description here

  • The flip of the matrix

    Flip left and right to exchange the first and last columns of the original matrix , The second column and the penultimate column exchange , And so on .

    • fliplr(A): The matrix A Turn left and right .
    • flipud(A): The matrix A Flip up and down .
  • Matrix inversion

    • For a square matrix A, If there is a square matrix of the same order B, bring AB=BA=I( Unit matrix ), said AB Inverse matrices of each other .
    • inv(A): Seek the square array A The inverse matrix .
2.3 Matrix evaluation

Matrix evaluation refers to the operation of a matrix , The result is still a numerical value .

  • The determinant value of a matrix

    • Think of a square matrix as a determinant , And evaluate it according to the rules of determinant , This value is called the value of the determinant corresponding to the square matrix .
    • det(A): Seek the square array A The corresponding determinant value .
  • The rank of a matrix

    • The number of linearly independent rows or columns of a matrix is called the rank of the matrix
    • rank(A): O matrix A The rank of
    • The rank of odd order magic square matrix is n, That is, the full rank matrix of odd order magic square matrix .
    • The rank of a double even order magic square matrix is n/2+2(n Yes, multiples , But not 4 Multiple )
    • The rank of double even order magic square matrix is 3( The doom is 4 Multiple )
  • Trace of matrix

    • Equal to the sum of the diagonal elements of the matrix , Also equal to the sum of the eigenvalues of the matrix .
    • trace(A): O matrix A The trace of
  • Norm of matrix

    • Used to measure the length of a matrix or vector in a sense .

    • Vectorial 3 A common norm :

      • vector 1— norm : The sum of the absolute values of the vector elements
      • vector 2— norm : The square root of the square sum of vector elements
      • vector — Infinite norm : The maximum of the absolute values of all vector elements
    • MATLAB A function for finding vector norm is :

      • norm(V,1): Calculate the vector V Of 1— norm .
      • norm(V) or norm(V,2): Calculate the vector V Of 2— norm .
      • norm(V,inf): Calculate the vector V The infinity of — norm .
    • Norm of matrix

      • matrix A Of 1— norm : The maximum value of the sum of absolute values of matrix column elements
      • matrix A Of 2— norm : matrix A Transpose and matrix A The square root of the maximum eigenvalue of the product matrix itself .
      • matrix A The infinity of — norm : The maximum value of the sum of the absolute values of all matrix row elements
    • The function of finding the norm of the matrix :

      MATLAB Provides 3 A function of matrix norm , Its function call format is exactly the same as that of finding the norm of the vector .

  • The condition number of a matrix

    Parameters used to describe matrix performance .

    matrix A The number of conditions is equal to A Norm and A The product of the norm of the inverse matrix of .

    The closer the condition number is to 1, The better the performance of the matrix , On the contrary, the worse the performance of the matrix .

    • cond(A,1): Calculation A Of 1— Conditional number under norm .
    • cond(A) or cond(A,2): Calculation A Of 2— Conditional number under norm .
    • cond(A,inf): Calculation A Infinity of — Conditional number under norm .
2.4 Eigenvalues and eigenvectors of matrices

Mathematical definition of matrix eigenvalues

set up A yes n Square matrix , If there are numbers m And nonzero n dimension Column vector x, bring Ax=mx establish , said m It's a matrix A One of the The eigenvalue (characteristic value) or Eigenvalues (eigenvalue),x Is an eigenvector corresponding to the eigenvalue .

Find the eigenvalue and eigenvector of the matrix

  • E=eig(A): O matrix A All eigenvalues of , Make up a vector E

  • [X,D]=eig(A): O matrix A All eigenvalues of , Form a diagonal matrix D, And generate a matrix X,X Each column is the corresponding eigenvector .

  • Geometric meaning between eigenvalues

    MATLAB Provides a eigshow function , You can demonstrate the vector on the unit circle x and Ax The relationship between .

 Insert picture description here

2.5 sparse matrix

Sparse matrix means 0 The number of elements is much more than that of non elements 0 Matrix of the number of elements .

  • How the matrix is stored

    • Full storage mode

      Store all the elements of the matrix in columns

    • Sparse storage

      Only the values and positions of non-zero elements of the matrix are stored , Line number and column number . When using sparse storage , The order in which matrix elements are stored does not change , It's also stored in column order .

  • Sparse storage

    • Conversion between full storage and sparse storage

      • A=sparse(S): The matrix S Into a sparse storage matrix A
      • S=full(A): The matrix A A matrix converted to full storage S
    • Directly establish sparse storage matrix

      • sparse Other call formats for functions :

        • sparse(m,n): Generate a m×n All the elements of are 0 The sparse matrix of
        • sparse(u,v,S): among u、v、s yes 3 A vector of equal length .S Is the non-zero element of the coefficient storage matrix to be established ,u(i)、v(i) Namely S(i) Row and column subscripts of .
      • Use spconvert Function directly establishes the coefficient storage matrix , Its calling format is :

        B=spconvert(A)

        A It's a m×3 or m×4 Matrix , Each line represents a non-zero element ,m Is the number of non-zero elements .

        • A(i,1) It means the first one i The row of a non-zero element
        • A(i,2) It means the first one i The column of a non-zero element
        • A(i,3) It means the first one i The real part of a nonzero element
        • A(i,4) It means the first one i The imaginary part of a nonzero element

        If all elements of the matrix are real numbers , Then there is no need to 4 Column

    • Sparse storage of banded sparse matrix

      There are two basic types of sparse matrices : Sparse matrix with irregular structure and sparse matrix with regular structure

      Banded sparse matrix refers to a matrix in which all non-zero elements are concentrated on the diagonal .

      • [B,d]=spdigs(A): From banded sparse matrix A Extract all non-zero diagonal elements and assign them to the matrix B And the position vectors of these non-zero diagonals d
      • A=spdiags(B,d,m,n): A coefficient storage matrix that generates a banded sparse matrix A, among m、n Is the number of rows and columns of the original banded sparse matrix , matrix B Of the i The column is the... Of the original banded sparse matrix i A non-zero diagonal , vector d Is the position of all non-zero diagonals of the original banded sparse matrix .
    • Sparse storage of identity matrix

      Unit matrix : Only diagonal elements are 1, The other elements are 0 Matrix .

      • speye(m,n): Return to one m×n Sparse storage identity matrix

版权声明
本文为[Kyoto Xiaobai]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231806540042.html