MATLAB often needs to perform a series of operations on data when processing data. This article mainly introduces operations such as arithmetic operations, relational operations, logical operations, transposition, inversion, accumulation or multiplication of matrices.
Table of contents
(1) Addition and subtraction in matrix operations
(2) Multiplication in matrix operations
(3) Division operation of matrix operation
(4) Multiplication in matrix operations
(5) Multiplication and division in dot operations
(6) Power operation in point operation
5. Perform an inverse operation on the matrix
(1) Solve the inverse matrix of the target
(2) Find the pseudo-inverse for non-square matrices and non-full-rank square matrices
1. Arithmetic operations
Arithmetic operations in MATLAB mainly include point operations and matrix operations, and the results obtained by performing point operations and matrix operations on two identical matrices may be completely different.
(1) Addition and subtraction in matrix operations
When performing addition and subtraction operations between two matrices, addition and subtraction operations are performed between elements of the same row and column in the matrix, where the dimensions of the two matrices must be the same (if the dimensions of the two matrices are not the same, then program will report an error).
The formula for adding two matrices with the same dimension can be written as follows, assuming that A and B are respectively:
The result after adding is:
In linear algebra, we until the addition and subtraction of matrices satisfy the commutative law A+B=B+A and the associative law (A+B)+C=A+(B+C), which are also applicable in MATLAB.
For example:
A=[45,23,62;61,73,89;13,15,84];
B=[59,49,91;18,39,20;8,71,49];
C=A+B
D=A-B
The result of the operation is as follows:
C =
104 72 153
79 112 109
21 86 133
D =
-14 -26 -29
43 34 69
5 -56 35
The principle of subtraction between matrices is similar to the addition of matrices, and the subtraction of matrices can be regarded as AB=A+(-B).
(2) Multiplication in matrix operations
The multiplication of matrices can be divided into multiplication of numbers and matrices and multiplication of matrices and matrices.
In the process of multiplying numbers and matrices, assuming that a real number k is multiplied by matrix A, the final result is kA, and each element of the matrix is multiplied by k times. Suppose the matrix A is:
After multiplying by k times the result is:
The implementation through MATLAB code is as follows:
A=[4,5,6;16,14,17;20,4,15];
B=4*A
The result of the operation is as follows:
B =
16 20 24
64 56 68
80 16 60
In the process of matrix-matrix multiplication, assume that A is an m×p matrix and B is a p×n matrix. If A and B are multiplied, an m×n matrix is finally obtained. Assuming C=A*B, then the element of row i and column j of C is the sum of the product of the element of row i of A and the element corresponding to column j of B. Here it is assumed that both A and B are 2×2 row matrices:
Then the result of multiplying A and B is:
Here, in order to simplify the description of the operation process, only the operation of two 2×2 matrices is written. Matlab codes can be used to calculate matrices with higher dimensions, for example:
A=[12,-5;6,7;1,4];
B=[15,9,10;8,11,2];
C=A*B
The result of the operation is as follows:
C =
140 53 110
146 131 74
47 53 18
It can be seen from the above results that the 3×2 matrix is multiplied by the 2×3 matrix to obtain a 3×3 matrix. In linear algebra, we until the multiplication of two matrices satisfy the associative law: (AB)C=A(BC), k(AB)=(kA)B, and the distribution rate: A(B+C)= AB+AC, where k represents a constant, and A, B, and C are matrices respectively.
(3) Division operation of matrix operation
In MATLAB, matrix division can be divided into left division (/) and right division (\), where A\B is equivalent , and A/B is equivalent . If A is a non-singular square matrix, then left division and right division are both can be realised.
For example:
A=[2,3,8;4,7,9;5,6,4];
B=[4,5,4;3,5,9;8,3,2];
C1=A/B
C2=A\B
The result of the operation is as follows:
C1 =
-0.6087 1.1304 0.1304
0.8333 0.6667 -0.1667
1.3551 -0.1594 0.0072
C2 =
4.3019 0.6981 -3.0566
-2.4906 -0.5094 2.7170
0.3585 0.6415 0.2453
It can be seen that for left division and right division, the final operation results are different.
(4) Multiplication in matrix operations
When performing the multiplication operation in the matrix operation, the nth power of the matrix is the operation of multiplying the matrix with itself. It should be noted that the matrix must be a square matrix. Assuming that A is a square matrix, then it can be derived: , , ... and so on, for example:
A=[2,1,4;5,2,1;4,6,2];
B=A^2
C=A^3
The result of the operation is as follows:
B =
25 28 17
24 15 24
46 28 26
C =
258 183 162
219 198 159
336 258 264
(5) Multiplication and division in dot operations
In MATLAB, matrix operations and point operations are different concepts. The point operation of a matrix is the multiplication operation of each element of the matrix with the matrix corresponding to the same row and column.
For example, perform matrix operations and point operations on two 3×3 matrices:
A=[2,4,7;3,1,4;5,2,6];
B=[2,1,4;5,2,1;4,6,2];
C=A.*B
D=A*B
E=A/B
F=A./B
The result of the operation is as follows:
C =
4 4 28
15 2 4
20 12 12
D =
52 52 26
27 29 21
44 45 34
E =
1.6154 -0.7692 0.6538
0.9744 0.2821 -0.0897
1.4103 0.4872 -0.0641
F =
1.0000 4.0000 1.7500
0.6000 0.5000 4.0000
1.2500 0.3333 3.0000
It can be seen from the above operation results that the matrix operation and the point operation are different values.
Among them, it should be noted that:
- When performing dot multiplication and dot division, the two matrices must have the same dimension, otherwise an error will be reported.
- When performing division operations in dot operations, the functions of ./ and .\ are the same, and the results obtained are also the same.
(6) Power operation in point operation
In MATLAB, if there are two matrices A and B, the matrix obtained when performing the power operation of the point operation is C, then the calculation method of the i-th row and j-th column of C is: .
For example:
A=[2,4,5;3,2,2];
B=[2,3,1;4,6,2];
C=A.^B
The result after the operation is as follows:
C =
4 64 5
81 64 4
In addition to ".^", MATLAB also provides a power function, which has the same effect as ".^". For example, using the above matrix again can get:
A=[2,4,5;3,2,2];
B=[2,3,1;4,6,2];
C=A.^B
D=power(A,B)
The running results are as follows:
C =
4 64 5
81 64 4
D =
4 64 5
81 64 4
It is not difficult to see through the comparison before and after that the calculation results of the two are the same. So the power function is equivalent to ".^".
2. Relational operations
Relational operators in MATLAB include ">" (greater than), "<" (less than), ">=" (greater than or equal to), "<=" (less than or equal to), "==" (equal to) and "~=" (not equal to) etc.
For example:
A=[12,45,18;19,24,28];
B=[12,10,19;24,29,14];
C=A>B
D=A<B
E=A>=B
F=A<=B
G=A==B
H=A~=B
The running results are as follows:
C =
2×3 logical 数组
0 1 0
0 0 1
D =
2×3 logical 数组
0 0 1
1 1 0
E =
2×3 logical 数组
1 1 0
0 0 1
F =
2×3 logical 数组
1 0 1
1 1 0
G =
2×3 logical 数组
1 0 0
0 0 0
H =
2×3 logical 数组
0 1 1
1 1 1
From the above results, it can be seen that the corresponding operations between the two matrices have performed relational operations. When the result of the relational operation is true, the result is returned as 1; when the result of the relational operation is false, the result is returned as 0.
3. Logical operations
Like most programming languages, logical operations are also provided in MATLAB. Logical operations mainly include the following operators "&" (and), "|" (or), "~" (not), and xor (exclusive or) several operators.
For example:
A=[1,0,1;0,1,1];
B=[1,1,1;0,0,0];
C=A|B
D=A&B
E=~A
F=xor(A,B)
The result of the operation is as follows:
C =
2×3 logical 数组
1 1 1
0 1 1
D =
2×3 logical 数组
1 0 1
0 0 0
E =
2×3 logical 数组
0 1 0
1 0 0
F =
2×3 logical 数组
0 1 0
0 1 1
Logical operations can also be used to verify the correctness of the results. For example, for the following matrix A, check all elements greater than 10 and even numbers:
A=[12,11,8;16,19,21];
C=(A>10)&(mod(A,2)==0)
The running results are as follows:
C =
2×3 logical 数组
1 0 0
1 0 0
4. Transpose of matrix
The matrix after exchanging each row of the matrix A with the columns of the same sequence is called a transpose matrix. In linear algebra, a transpose matrix is generally used to represent.
Suppose the original matrix of A is:
The transposed matrix is:
MATLAB's operation for the transpose of a matrix is as follows:
A=[3,4,7;4,3,1];
B=A.'
The result of the operation is as follows:
B =
3 4
4 3
7 1
5. Perform an inverse operation on the matrix
(1) Solve the inverse matrix of the target
When performing data processing on matrices, we often need to perform inverse operations on them. Suppose AB=BA=E (E is the unit matrix), then B is the inverse matrix of A, and A is also the inverse matrix of B. In MATLAB The inv function is provided to invert the matrix.
In linear algebra, we have the following conclusions: , which can be verified by MATLAB:
A=[3,4,6,7;23,4,5,16;8,39,2,10;4,5,17,21];
B=inv(A)
C=inv(inv(A))
The result obtained after the operation is as follows:
B =
0.3233 0.0295 -0.0208 -0.1203
0.0815 -0.0136 0.0222 -0.0274
0.8130 -0.0522 -0.0515 -0.2067
-0.7391 0.0399 0.0404 0.2444
C =
3.0000 4.0000 6.0000 7.0000
23.0000 4.0000 5.0000 16.0000
8.0000 39.0000 2.0000 10.0000
4.0000 5.0000 17.0000 21.0000
There are also the following conclusions in linear algebra: , and MATLAB can be used to verify and solve:
A=[13,4,5;17,23,10;8,5,7];
B=[5,9,11;14,18,20;31,34,33];
C=inv(A*B)
D=inv(B)*inv(A)
The running results are as follows:
C =
-0.1511 0.0670 -0.0165
0.2673 -0.1576 0.1199
-0.1373 0.0981 -0.0992
D =
-0.1511 0.0670 -0.0165
0.2673 -0.1576 0.1199
-0.1373 0.0981 -0.0992
It can be seen from the running results that the results are the same, which can verify the correctness of the conclusion.
In linear algebra, if the matrix A is reversible and the following rules are met: , it is verified by MATLAB code, assuming :
a=2;
A=[5,6,8;4,3,9;7,8,2]
B=inv(a*A)
C=a^(-1)*inv(A
The running results are as follows:
B =
-0.3750 0.2955 0.1705
0.3125 -0.2614 -0.0739
0.0625 0.0114 -0.0511
C =
-0.3750 0.2955 0.1705
0.3125 -0.2614 -0.0739
0.0625 0.0114 -0.0511
It can be seen that the results are the same, indicating that the conclusion is correct.
In addition to the inv function, the inverse matrix of the matrix can also be obtained by calculating the -1 power of the matrix.
(2) Find the pseudo-inverse for non-square matrices and non-full-rank square matrices
When the matrix A is not a square matrix, the matrix does not have an inverse matrix, but MATLAB provides the pinv function to find a pseudo-inverse operation for a matrix that is not a target and a square matrix that is not full rank. The so-called pseudo-inverse is that MATLAB finds a The transposed matrix of the same row and column satisfies the following conditions: ABA=A, BAB=B, at this time B is the pseudo-inverse of A.
For example:
A=[2,5,4;5,6,8];
B=pinv(A)
The result of the operation is as follows:
B =
-0.2494 0.1837
0.4376 -0.2041
-0.1723 0.1633
6. Sum and product
In MATLAB, you can perform summation and product operations on all the data of the matrix, which is convenient for operation in the process of data processing. The following will introduce the summation and product respectively.
(1) Sum
The sum function is provided in MATLAB for summing data. For the sum function, there are the following operation rules (where V represents a vector and A represents a matrix):
- sum(V): Calculates the cumulative sum of all elements in a vector.
- sum(A): Returns a row vector, and each element of the row vector calculates the sum of each column of matrix A.
- sum(A,num): When the value of num is 1, it has the same function as sum(A); when the value of num is 2, it returns a column vector, and each element of the column vector corresponds to is the sum of each row in matrix A.
- sum(sum(A)): Sums all elements of the matrix as a whole.
For example, to sum a vector:
V=[34,24,5,6,31,23,45,23];
B=sum(V)
The result of the operation is as follows:
B =
191
For example, to perform a summation operation on a matrix:
A=[3,4,7,8;8,5,3,1;9,5,10,12];
B=sum(A)
C=sum(A,1)
D=sum(A,2)
E=sum(sum(A))
The result of the operation is as follows:
B =
20 14 20 21
C =
20 14 20 21
D =
22
17
36
E =
75
As can be seen from the above results, sum(A) and sum(A,1) return a row vector, calculate the sum of each column in the matrix, sum(A,2) returns a column vector, and calculate the matrix The sum of each row in the matrix, and sum(sum(A)) calculates the sum of all elements in the matrix.
(2) quadrature
The prod function is provided in MATLAB to perform quadrature operations on data. For the prod function, there are the following operation rules (where V represents a vector and A represents a matrix):
- prod(V): Computes the cumulative product of all elements in a vector.
- prod(A): Returns a row vector, and each element of the row vector calculates the product of each column of matrix A.
- prod(A, num): When the value of num is 1, the function is the same as prod(A); when the value of num is 2, a column vector is returned, and each element of the column vector corresponds to is the product of each row in matrix A.
- prod(prod(A)): Calculates the product of all elements of the matrix as a whole.
For example:
For example, to perform a quadrature operation on a matrix:
A=[3,4,7,8;8,5,3,1;9,5,10,12];
B=prod(A)
C=prod(A,1)
D=prod(A,2)
E=prod(prod(A))
The result of the operation is as follows:
B =
216 100 210 96
C =
216 100 210 96
D =
672
120
5400
E =
435456000
As can be seen from the above results, prod(A) and prod(A,1) return a row vector, calculating the product of each column in the matrix, and prod(A,2) returns a column vector, calculating the matrix The product of each row in , while prod(prod(A)) calculates the product of all elements in the matrix.