[This little article is perfect! 】One article to see through, MATLAB | Arrays and matrices super detailed entry advanced must read

Table of contents

introduce

First, the creation and operation of arrays

Via: Create a one-dimensional array

Create a one-dimensional array through the logspace function

Create a one-dimensional array through the linspace function

Second, the operation of the array

Relational Operations on Arrays

Logical Operations on Arrays

3. Matrix

Matrix construction

Subscript references to matrices

matrix size

Fourth, the operation of matrix elements

matrix difference

Matrix Operations

matrix analysis

Matrix factorization


Benefits: There is a full set of MATLAB information at the end of the article

introduce

It can be said that array operations are the basis of MATLAB calculations, and numerical arrays are the most important built-in data types of MATLAB. The matrix itself is a special array . MATLAB is meant to be a matrix laboratory, which shows the importance of arrays and matrices.

First, the creation and operation of arrays

, Elements in the same row in the array are separated  by commas  or spaces, and semicolons ;are used between different rows.

  1. >> A = [6 5 4 3 2 1]
    
    A = 6 5 4 3 2 1
    
    >> B = [6, 5, 4, 3, 2, 1]
    
    B = 6 5 4 3 2 1
    
    >> C = [6;5;4;3;2;1]
    
    C =
    
        6
    
        5
    
        4
    
        3
    
        2
    
        1
    
    >> D = B' % 转置
    
    D =
    
        6
    
        5
    
        4
    
        3
    
        2
    
        1

Note that accessing array elements uses parentheses () , not what most programming languages ​​use  []. In addition,  the MATLAB array subscript starts from 1 , and it is also allowed to pass in a subscript list to realize the query and modification of multiple elements. (This is consistent with Julia):

 
 
  1. >> A = [6 5 4 3 2 1]
    
    A = 6 5 4 3 2 1
    
    >> A(3) = 0
    
    A = 6 5 0 3 2 1
    
    >> A([1 4]) = [1 1]
    
    A = 1 5 0 1 2 1

Via: Create a one-dimensional array

In MATLAB, you can  : create a one-dimensional array  by creating a one-dimensional array X=A:step:B, where  A is the first variable to create a one-dimensional array, and  step is the value that is incremented or decremented each time, until the absolute value of the difference between the last element and B is less than or equal to the  step absolute value. If not specified  step, the default is to increase by one unit length.

 
 
  1. >> 2:6
    
    ans = 2 3 4 5 6
    
    >> 2.1:1.5:6
    
    ans = 2.1000 3.6000 5.1000
    
    >> 2.1:6
    
    ans = 2.1000 3.1000 4.1000 5.1000
    
    >> 2.1:-1.5:-6
    
    ans = 2.1000 0.6000 -0.9000 -2.4000 -3.9000 -5.4000

Create a one-dimensional array through the logspace function

  • y=logspace(a,b): Create a row vector  ywith the first element being 10^a and the last element being 10^b, forming a geometric sequence with a total of 50 elements.

  • y=logspace(a,b,n): This function creates a row vector  ywith the first element being 10^a and the last element being 10^b, forming a geometric sequence with a total of n elements .

 
 
  1. >> A = logspace(1, 2, 10)
    
    A = 10.0000 12.9155 16.6810 21.5443 27.8256 35.9381 46.4159 59.9484 77.4264 100.0000

Create a one-dimensional array through the linspace function

  • y=linspace(a,b): Creates a row vector  ywith a as the first element and b as the last element, forming a linearly spaced vector (arithmetic sequence) with a total of 100 elements.

  • y=logspace(a,b,n): This function creates a row vector  ywith the first element a and the last element b forming a linearly spaced vector (arithmetic sequence) of n elements in total .

 
 
  1. >> A = linspace(1, 100)
    
    A =
    
     1 至 21 列
    
        1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
    
     22 至 42 列
    
       22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    
     43 至 63 列
    
       43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    
     64 至 84 列
    
       64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    
     85 至 100 列
    
       85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    
    >> B = linspace(1, 36, 1)
    
    B =
    
       36

Second, the operation of the array

The operation of an array starts from the entire element of the array and performs an operation on each element in it. Basic operations include addition, subtraction, multiplication, left division, right division, and exponentiation.

 
 
  1. >> A = [1 5 6 8 9 6]
    
    A = 1 5 6 8 9 6
    
    >> B = [9 85 6 2 4 0]
    
    B = 9 85 6 2 4 0
    
    >> C = [1 1 1 1 1]
    
    C = 1 1 1 1 1
    
    >> D = A + B
    
    D = 10 90 12 10 13 6
    
    >> E = A - B
    
    E = -8 -80 0 6 5 6
    
    >> F = A * 2
    
    F = 2 10 12 16 18 12
    
    >> G = A + 3
    
    G = 4 8 9 11 12 9
    
    >> H = A - G
    
    H = -3 -3 -3 -3 -3 -3

It should be noted that the multiplication of arrays is different from the multiplication of matrices . It requires  the same dimension as the sumA  ,  B and the operation is the multiplication operation of the corresponding elements of the array, and the calculation result is an array with the   same  dimension as the sum. Naturally, for division, the corresponding elements are divided.AB

 
 
  1. >> A = [1 2 3]
    
    A = 1 2 3
    
    >> B = [2 3 4]
    
    B = 2 3 4
    
    >> C = A .* B % 数组的点乘
    
    C = 2 6 12
    
    >> D = A * 3 % 数组与常数的乘法
    
    D = 3 6 9

Array division:

 
 
  1. >> A = [1 2 3]
    
    A = 1 2 3
    
    >> B = [2 3 4]
    
    B = 2 3 4
    
    >> A ./ B % 左除
    
    ans = 0.5000 0.6667 0.7500
    
    >> A .\ B % 右除,等价于 B ./ A
    
    ans = 2.0000 1.5000 1.3333
    
    >> B / 2
    
    ans = 1.0000 1.5000 2.0000

Array powers:

 
 
  1. >> A = [1 2 3 4]
    
    A = 1 2 3 4
    
    >> B = [1 2 3 4]
    
    B = 1 2 3 4
    
    >> C = A .^ B % 数组的乘方
    
    C = 1 4 27 256
    
    >> D = 3 .^ A % 常数与数组的乘方
    
    D = 3 9 27 81

The dot product operationdot()  of the array can be realized through the function  , but the operation rule requires that the dimensions of the array   and   are the same, and the calling format is:AB

  • C=dot(A,B)

  • C=dot(A,B,dim)

 
 
  1. >> A = [1 2 3 4]
    
    A = 1 2 3 4
    
    >> B = [1 2 3 4]
    
    B = 1 2 3 4
    
    >> dot(A, B)
    
    ans = 30

Relational Operations on Arrays

MATLAB provides 6 array relational operators, namely  <(less than), (less than  <=or equal to), (greater than  >),  >=(greater than or equal to),  ==(equal to),  ~=(not equal to). Operation rules:

  • If the size of the scalars is directly compared and the relationship is established, 1 is returned, otherwise 0 is returned.

  • When the comparison value is an array with equal dimensions, compare the elements at the same position one by one, and give the comparison result. The end result is an array of 0 or 1 elements with the same dimensions as the compared arrays.

 
 
  1. >> A = [1 2 3 4 5]
    
    A = 1 2 3 4 5
    
    >> B = [0 2 3 4 5]
    
    B = 0 2 3 4 5
    
    >> A > B
    
    ans =
    
     1×5 logical 数组
    
      1 0 0 0 0
    
    >> A >= 3
    
    ans =
    
     1×5 logical 数组
    
      0 0 1 1 1

Logical Operations on Arrays

Arrays in MATLAB provide three logical operators:  &(and),  |(or) and  ~(not). True if it is a non-zero element, denoted by 1. Zero elements are false, represented by 0.

 
 
  1. >> A = [1 2 3 4 5 6]
    
    A = 1 2 3 4 5 6
    
    >> B = [0 1 2 3 4 5]
    
    B = 0 1 2 3 4 5
    
    >> A & B
    
    ans =
    
     1×6 logical 数组
    
      0 1 1 1 1 1
    
    >> A | B
    
    ans =
    
     1×6 logical 数组
    
      1 1 1 1 1 1
    
    >> ~B
    
    ans =
    
     1×6 logical 数组
    
      1 0 0 0 0 0

3. Matrix

There are many differences between matrices and arrays in MATLAB, such as:

  • Matrix is ​​a mathematical concept, while array is a concept in the field of computer programming;

  • As a manifestation of transformation or mapping operators in mathematics, matrix operations have clear and strict mathematical rules . Array operation is a rule defined by MATLAB software, the purpose is to make data management more convenient.

The connection between the two is that the matrix exists in the form of an array. Therefore, a one-dimensional array is equivalent to a vector, and a two-dimensional array is equivalent to a matrix, in other words, a matrix is ​​a subset of an array .

Matrix construction

There are two ways to construct a matrix. One is similar to a cell array and can directly assign values ​​to variables; the other is to use a pre-provided special matrix command.

function name function function
ones(n) n×n 1 matrix (elements are all 1)
zeros(n) n×n 0 matrix (elements are all 0)
eye(n) n×n identity matrix
eye(m,n) m×n identity matrix
rand(n) n×n matrix, uniformly distributed random numbers between elements 0-1
diag(x) The main diagonal elements are taken from the vector  xand the remaining elements have the value 0
triu(A) Construct the same upper triangular matrix as A , the elements on the main diagonal are the corresponding elements in A, and the rest are 0
tril(A) Construct the same lower triangular matrix as A , the elements on the main diagonal are the corresponding elements in A, and the rest are 0

There are other matrix types, such as Rubik's cube  magic, Pascal  pascal, Vandermonde, etc.

 
 
  1. >> A = ones(3)
    
    A =
    
        1 1 1
    
        1 1 1
    
        1 1 1
    
    >> B = rand(3)
    
    B =
    
       0.8147 0.9134 0.2785
    
       0.9058 0.6324 0.5469
    
       0.1270 0.0975 0.9575
    
    >> C = [1 2 3; 4 5 6; 7 8 9]
    
    C =
    
        1 2 3
    
        4 5 6
    
        7 8 9
    
    >> triu(C)
    
    ans =
    
        1 2 3
    
        0 5 6
    
        0 0 9

Subscript references to matrices

The index of ordinary two-dimensional array elements is divided into double subscript index and single subscript index. The double subscript index index corresponds to the row and column position of the element in the matrix through a binary array, for example: A(2, 3) indicates the element in the second row and third column in the matrix A. The way of single subscript index is to adopt the principle of  column element priority ,  and reorganize the matrix of m rows and  A (5) represents the element of the third row and the second column.n

expression Function
A(1) Reorganize the two-dimensional matrix A into a one-dimensional array, and return the first element in the array
A(:,j) Returns the jth column vector in the two-dimensional matrix A
A(i,:) Returns the i-th row vector in the two-dimensional matrix A
A(:,j:k) Returns the submatrix composed of column vectors from column j to column k in the two-dimensional matrix A
A(i:k,:) Returns the sub-matrix consisting of row vectors from the i-th row to the k-th row in the two-dimensional matrix A
A(:) Merge each column in matrix A into one long column vector

Matrix structure test:

name Function
isempty(A) Check if matrix is ​​empty
isscalar(A) Checks if matrix is ​​a scalar matrix with one element
isvector(A) 1D vector that detects whether matrix has only one row or one column of elements
issparse(A) Check if an array is a sparse matrix
 
 
  1. >> A = magic(3)
    
    A =
    
        8 1 6
    
        3 5 7
    
        4 9 2
    
    >> A(5)
    
    ans = 5
    
    >> B = [1]
    
    B = 1
    
    >> isempty(B)
    
    ans =
    
     logical
    
      0
    
    >> isscalar(B)
    
    ans =
    
     logical
    
      1

matrix size

The size of the matrix usually includes the following aspects: the number of dimensions, the length of each dimension, and the number of matrix elements. To this end, MATLAB provides four functions, which are used to obtain information about the shape of the matrix.

calling format Function
n=ndims(X) Get the number of matrices
[m,n]=size(X) Get the length of the matrix in each dimension
n=length(X) Get the length of the longest dimension of a matrix
n=numel(X) Get the number of matrix elements
 
 
  1. >> A = eye(5, 3)
    
    A =
    
        1 0 0
    
        0 1 0
    
        0 0 1
    
        0 0 0
    
        0 0 0
    
    >> ndims(A)
    
    ans = 2
    
    >> length(A)
    
    ans = 5
    
    >> numel(A)
    
    ans = 15

One of the keys to optimizing MATLAB code is understanding the memory usage of matrices . You can use to  whos view all the information of the specified variable in the current workspace, including variable name, matrix size, memory usage, data type, etc.

 
 
  1. >> m = rand(2)
    
    m =
    
       0.9649 0.9706
    
       0.1576 0.9572
    
    >> whos m
    
     Name Size Bytes Class Attributes
    
     m 2x2 32 double

Fourth, the operation of matrix elements

To use matrix operations, you need to follow the mathematical definition of a matrix. For example, the premise of matrix addition and subtraction operations is that two or more matrices participating in the operation must have the same number of rows and columns .

Satisfied: commutative law A+B=B+A, associative law A+(B+C)=(A+B)+C, existence of zero element A+0=0+A=A, existence of negative element A+(-A) =(-A)+A

Matrix multiplication operations include multiplication of numbers and matrices, and multiplication of matrices. The multiplication of matrices must satisfy the fact that the number of columns of the multiplied matrix is ​​equal to the number of rows of the multiplied matrix . In addition, the multiplication between matrices does not follow the commutative law , but satisfies the following operation law:

  • Associative law: (A×B)×C=A×(B×C)

  • Left distributive law: A×(B+C)=A×B+A×C

  • Right distributive law: (B+C)×A=B×A+C×A

  • Existence of identity matrix: E×A=A, A×E=A

Matrix division is the inverse operation of multiplication, divided into left division and right division. Use operator symbols  \ and  / representations. For general two-dimensional matrices  A  and  B , when performing A\B operation,   the number of rows of  A is required  to be equal to the number of rows of B ; when performing A/B operation, the number of columns of A and B is required to be equal.

 
 
  1. >> A = [1 2; 1 3];
    
    >> B = [1 0; 1 2];
    
    >> C = A / B
    
    C =
    
            0 1.0000
    
      -0.5000 1.5000
    
    >> D = A\B
    
    D =
    
        1 -4
    
        0 2
    
    >> C * B
    
    ans =
    
        1 2
    
        1 3
    
    >> A * D
    
    ans =
    
        1 0
    
        1 2

When the matrix is ​​a square matrix, the power operation of the matrix can be performed, and MATLAB uses it  ^ to represent the power operation:

 
 
  1. >> A = magic(3)
    
    A =
    
        8 1 6
    
        3 5 7
    
        4 9 2
    
    >> A2 = A^2
    
    A2 =
    
       91 67 67
    
       67 91 67
    
       67 67 91

matrix difference

diff() The function is to calculate the difference of the matrix. In short, the difference between the previous row (column) and the next row (column) in the matrix is ​​arranged in the corresponding position of the previous row (column).

  • Y=diff(X,n): Computes the nth-order difference of elements in each column of the matrix.

  • Y=diff(X,n,dim)dim : Computes the difference of elements of  a matrix in a given dimension  n . At that time dim=1 , the difference of the elements of each column of the matrix is ​​calculated. At that time dim=2 , the difference of the elements of each row of the matrix is ​​calculated.

 
 
  1. >> A = magic(3)
    
    A =
    
        8 1 6
    
        3 5 7
    
        4 9 2
    
    >> B = diff(A)
    
    B =
    
       -5 4 1
    
        1 4 -5
    
    >> C = diff(A, 2)
    
    C =
    
        6 0 -6
    
    >> D = diff(A, 1, 2)
    
    D =
    
       -7 5
    
        2 2
    
        5 -7

Matrix Operations

Matrix operations are an extremely important part of linear algebra, including matrix analysis, eigenvalue solving, and singular values.

matrix analysis

The matrix analysis functions provided by MATLAB are as follows:

Function name describe
norm Find the norm of a matrix or vector
normest Estimate the 2nd norm of the matrix
rank Rank of the matrix
det determinant of matrix
trace The trace of the matrix, that is, the sum of the diagonal elements
null 0 space
orth Orthogonalization space
rref Reduced Row Echelon Form
subspace find the angle of two matrix spaces
 
 
  1. >> norm(1:6, 2) % 2阶范数
    
    ans = 9.5394
    
    >> normest(1:6)
    
    ans = 9.5394

When the dimension of the matrix is ​​relatively large, it will take a long time to calculate the norm, and when an approximate norm value meets the requirements, you can consider using a function to  normset() estimate the second-order norm value.

 
 
  1. >> A = [1 2 3; 3 4 5; 7 8 9];
    
    >> B = magic(3);
    
    >> r1 = rank(A)
    
    r1 = 2
    
    >> r2 = rank(B)
    
    r2 = 3
    
    >> det(B)
    
    ans = -360
    
    >> trace(B)
    
    ans = 15
    
    >> Z = null(A)
    
    Z =
    
       0.4082
    
      -0.8165
    
       0.4082

Matrix factorization

Matrix decomposition is to decompose a matrix into several "simple" matrix multiplication forms. Matrix decomposition is very important both in theory and in engineering applications.

name describe
chol Cholesky decomposition
choline Incomplete Cholesky Decomposition of Sparse Matrix
lu Matrix LU decomposition
luinc Incomplete LU factorization of sparse matrices
qr Orthogonal Triangulation Decomposition
svd singular value decomposition
gsvd General Singular Value Decomposition
schur Schur decomposition

Show Charging Jun will bring you the latest and most comprehensive interpretation as soon as possible, don't forget the triple wave.  

                                                   

 

Pay attention to the official account: Resource Charging Bar
Reply: Chat GPT
Charging Jun sent you: Enjoy using the Chinese version for free
Click on the small card to follow, reply: IT

I have all the information I want 

Guess you like

Origin blog.csdn.net/CDB3399/article/details/131225316