MATLAB matrix processing

table of Contents

1. Special matrix

2. Norms of vectors and matrices

3. Sparse matrix (the number of zero elements is far more than the number of non-zero elements)


1. Special matrix

zeros function: generate a matrix of all zeros, that is, a zero matrix;

ones function: generate a matrix of all ones, that is, a unitary matrix;

Eye function: Generate a matrix with a diagonal of 1. When the matrix is ​​a square matrix, an identity matrix is ​​obtained; 

rand function: generate a random matrix uniformly distributed in the interval (0, 1);

randn function: Generate a standard normal distribution random matrix with a mean of 0 and a variance of 1.

The calling format of the zeros function:

zeros(m): Generate m×m zero matrix;

zeros(m,n): Generate m×n zero matrix;

zeros(size(A)): Generate a zero matrix of the same size as matrix A.

2. Norms of vectors and matrices

The norm of a matrix or vector is used to measure the length of a matrix or vector in a certain sense.

In MATLAB, the function for finding the norm of a vector is:

norm(V) or norm(V,2): calculate the 2-norm of vector V;

norm(V,1): Calculate the 1-norm of the vector V;

norm(V,inf): Calculate the ∞-norm of the vector V.

MATLAB provides functions for finding the norm of three kinds of matrices, and the function call format is exactly the same as the function for finding the norm of a vector.

3. Sparse matrix (the number of zero elements is far more than the number of non-zero elements)

3.1 How to store the matrix

(1) Complete storage method: store all elements of the matrix in columns.

(2) Sparse storage mode: Only store the value and position of the non-zero elements of the matrix, that is, the row number and column number.

Note: When sparse storage is used, the storage order of matrix elements has not changed, and storage is performed in column order.

3.2 The generation of sparse storage

(1) Conversion between full storage and sparse storage

A=sparse(S): Convert matrix S to matrix A in sparse storage mode.

S=full(A): Transform matrix A into matrix S in full storage mode.

(2) Directly build a sparse storage matrix

sparse(m,n): Generate an m×n sparse matrix with all elements zero.

sparse(u,v,S): where u, v, and S are 3 vectors of equal length.

S is the non-zero element of the sparse storage matrix to be established, u(i) and v(i) are the row and column subscripts of S(i) respectively.

(3) Sparse storage of striped sparse matrix

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

[B,d]=spdiags(A): Extract all non-zero diagonal elements from the striped sparse matrix A and assign them to the matrix B and the position vectors d of these non-zero diagonals;

A=spdiags(B,d,m,n): Generate the sparse storage matrix A of the band-shaped sparse matrix, where m and n are the number of rows and columns of the original band-shaped sparse matrix, and the i-th column of matrix B is the original The ith non-zero diagonal of the band-shaped sparse matrix, and the vector d is the position of all the non-zero diagonals of the original band-shaped sparse matrix.

(4) Sparse storage of identity matrix

speye(m,n) returns an m×n sparse storage identity matrix.

The content comes from MOOC courses: scientific computing and MATLAB language

 

Guess you like

Origin blog.csdn.net/weixin_45317919/article/details/109194254