MATLAB is a powerful matrix computation tool that supports various matrix operations, including basic operations, linear algebra operations, and advanced applications. Below are some common matrix operations along with their example code.
1. Basic Matrix Operations
(1) Addition and Subtraction
The addition and subtraction of two matrices require the same dimensions, and the operation is performed element-wise.
A = [1, 2; 3, 4];
B = [5, 6; 7, 8];
C_add = A + B; % Matrix addition
C_sub = A - B; % Matrix subtraction
(2) Multiplication
- Matrix multiplication: use *, requiring the number of columns in the first matrix to equal the number of rows in the second matrix.
- Element-wise multiplication: use .*, multiplying corresponding elements.
C_mul = A * B'; % Matrix multiplication
C_elem_mul = A .* B; % Element-wise multiplication
(3) Division
- Left division \ and right division / are used to solve linear equations.
- Element-wise division uses ./ or .
.
X_left = A \ B; % Left division
X_right = A / B; % Right division
X_elem_div = A ./ B; % Element-wise right division
2. Linear Algebra Operations on Matrices
(1) Transpose – swaps the rows and columns of the matrix.
A_transpose = A';
(2) Inversion – applicable only to non-singular square matrices.
A_inv = inv(A);
(3) Determinant – calculates the determinant of a square matrix, used to determine if the matrix is invertible.
det_A = det(A);
(4) Rank – calculates the maximum number of linearly independent rows or columns in the matrix.
rank_A = rank(A);
🎀
Thank you for your attention~
I hope the above content is helpful to you!
🎀