matrix
Table of contents
2. References to matrix elements
1. Colon expressions
e1:e2:e3 omit the step size e2, then the step size is 1
linspace(a,b,n) automatically generates 100 elements when n is omitted
a: the first element
b: last element
n: total number of elements
2. References to matrix elements
In MATLAB, matrix elements are stored by column, that is, the first column element of the matrix is stored first, then the second column... until the last column element of the matrix. The serial number of the matrix element is the order in which the matrix elements are arranged in memory.
example:
Submatrix :
A(i,:) All elements of row i
A(;,j) all elements in column j
A(i:i+m,k:k+m) All elements in row i~i+m and column k~k+m
A(i:i+m,:) All elements of row i~i+m
An empty matrix is a matrix without any elements: x=[]
Change the shape of the matrix :
reshape(A,m,n): On the premise that the total elements of the matrix remain unchanged, the matrix A is rearranged into a two-dimensional matrix of m X n.
A(:) stacks each column of matrix A into a column vector.
3. Matrix operations
3.1 Arithmetic operations
Transpose |
add |
reduce |
take |
right division |
left division |
power |
|
matrix operator |
A’ |
+ |
- |
* |
/ |
^ |
|
scalar operator |
A.’ |
+ |
- |
.* |
./ |
. |
.^ |
There are two types of matrix division operations, right division / and left division
If the A matrix is a non-singular square matrix, then B/A is equivalent to B*in(A), and AB is equivalent to inv(A)*B
Power operation: A^x, requiring A to be the target and x to be a scalar
3.2 Logical operations
relational operator |
< |
<= |
> |
>= |
== |
~= |
alphabetical operator |
lt |
the |
gt |
ge |
eq |
it is |
Logical Operators |
& |
| |
~ |
xor |
alphabetical operator |
and |
or |
not |
xor |
4. Special Matrix
zeros(m): generate m*m zero matrix
zeros(m,n): generate m*n zero matrix
zeros(size(A)): Generates a zero matrix of the same size as matrix A
magic(n): Generate a magic square matrix of order n, the sum of each row, column and n elements on the main and auxiliary diagonals is equal
vander(v): Generate a Vandermonde matrix based on the vector v
hilb(n): generate n-order Hilbert matrix
Compan(p): Generate a companion matrix, where p is a polynomial coefficient vector, the high power coefficients are ranked first, and the low power coefficients are ranked last
pascal(n): generate an n-order Pascal matrix
5. Matrix transformation
5.1 Diagonal Matrix
Diagonal matrix: a matrix with only non-zero elements on the diagonal
Quantity matrix: Diagonal matrix with equal elements on the diagonal
Identity matrix: Diagonal matrix in which all elements on the diagonal are 1
diag(v): Take the vector v as the main diagonal element to generate a diagonal matrix
diag(v,k) takes the vector v as the kth diagonal element to generate a diagonal matrix
5.2 Triangular array
upper triangular matrix
triu(A): Extract the elements on and above the main diagonal of matrix A.
triu(A,k): Extract the elements of the kth main diagonal of matrix A and above.
lower triangular matrix
tril(A): Extract the main diagonal of matrix A and the elements below it.
tril(A,k): Extract the elements of the kth main diagonal of matrix A and below.
5.3 Matrix rotation
rot90(A,k): Rotate the matrix A counterclockwise by k times. When k is 1, it can be omitted.
5.4 Sparse Matrix
Complete storage method: store all elements of the matrix in columns
Sparse storage method: only store the value and position of the non-zero elements of the matrix, that is, row number and column number
A=sparse(S): Transform matrix S into matrix A with sparse storage
S=full(A): Transform matrix A into matrix S in full storage mode
sparse(m,n): Generate a sparse matrix in which all elements of mxn are 0.
sparse(u, v, s): where u, v, S are 3 equal-length vectors, S is the non-zero element of the sparse storage matrix to be established, u(i), v(i) are S(i ) row and column subscripts
[B,d]=spdiags(A): Extract all non-zero diagonal elements from the banded sparse matrix A and assign them to matrix B and the position vector d of these non-zero diagonals.
speye(m,n); returns a m*n sparse storage identity matrix.
6. Matrix evaluation
det(A): the value of the determinant corresponding to the square matrix A
inv(A): Find the inverse of the matrix
rank(A): Find the rank of the matrix
trace(A): Find the trace of the matrix
The trace of a matrix is equal to the sum of the diagonal elements of the matrix and also the sum of the eigenvalues of the matrix
The norm of a matrix or vector is used to measure the length of a matrix or vector in a certain sense
norm(v) or norm(v,2): Calculate the 2-norm of the vector v
norm(v,1): Calculate the 1-norm of the vector v
norm(v,inf): Calculate the -norm of the vector v
The condition number of the matrix is equal to the product of the norm of A and the norm of the inverse matrix of A.
cond(A,1): Calculate the condition number under the 1-norm of A.
cond(A) or cond(A,2): Calculate the condition number under the 2-norm of A.
cond(A,1): Calculate the condition number under the norm of A.
Find the eigenvalues and eigenvectors of a matrix
E=eig(A): Find all the eigenvalues of the matrix A to form a vector E.
[X,D]=eig(A): Find all the eigenvalues of the matrix A, form a diagonal matrix D, and generate a matrix X, and each column of X is the corresponding eigenvector.