%1 generates special matrices, 0 matrices, all 1 unitary matrices, identity matrices, random matrices, standard normal distribution random matrices
%zeros();ones();eye();rand();randn();
zeros(3 );
zeros(3,2);
A=[1:1:4;1:1:4];
%[1:9];(1:9);1:9;A[1:9]
A( 1,2);% One-dimensional access to two-dimensional, get 1, 1 is arranged in sequence
B=[1,2,3;1,2,3];
C=A(end-1:end,end-1: end);
D=[B([1,3]);A([1,2]),A(7:9)];
zeros(size(A));
x=20+(50-20)*rand(5);%y=a+(ba)x, establish a random matrix with k-order interval (a, b) y=0.6+sqrt(0.1)*randn(5
) ;%y=μ+ox, establish a normal distribution random matrix with mean μ and variance 0.1k order
% random integer and random matrix integer
A=fix((90-10+1)*rand(1)+10); %rand(1) gets a first-order random number, also a random integer
% Rubik's cube matrix, each row and column, the sum of the two diagonals is (n^2+1)n/2
% Fill the numbers in the range [101,125] into the 5x5 matrix, the sum is 565, consider 5 The sum of the order matrices is 65, add 100 to each number, which is 565
M=100+magic(5);
% Vandermond matrix, the last column is all 1, the second to last column is the specified matrix, and the others are the product of the last column and the second to last column
E=vander([1;2;3;5]);
%Hilbert matrix
format rat %rational output
H=hilb(4);
H=invhilb(4);
format
%Toeplitz matrix , the first row and column are defined by yourself, and each other element is the same as the upper left element
toeplitz(1:6)
toeplitz(1:6,1:5)
%Adjoint matrix , the adjoint matrix of polynomials, is composed of polyphase coefficient vectors
%Find x^3-7x+6
p=[1,0,-7,6];
compan(p);
%Pascal matrix
Z=pascal(6);
inv(Z)
%Diagonal matrix extracts the main diagonal element
A=[1:3;4:6];
D1=diag(A); %diag(A,k), the diagonal can be offset upward or downward
D=diag ([1,1,1,1]); % Construct a diagonal matrix, a diagonal matrix of mxm, or it can be followed by parameters (V, k) and the diagonal offset of other elements is 0 diag(diag(A)
) ;% Generate a diagonal matrix with the main diagonal elements of A
%Triangular matrix
%Upper triangle triu(A,k) Lower triangle tril(A,k)
%Matrix transpose
B=[1:3;4:6;7:9];
B1=B.';
B2=B'; %Conjugate transpose, if it is a real number, the two results are the same, equivalent to conj (A).'
% Matrix rotation and flip left, right, up and down
B3=rot90(B,1); % Rotate 90° counterclockwise
B4=rot90(B3,-1); % Rotate 90° clockwise
B5=fliplr(B4); % Flip left and right
B6= flipud(B5);% flip up and down, rotate 180 and then flip vertically, which is equal to transpose
%Inverse and pseudo-inverse
A=[1:3;4:6];
A1=inv(A); %Find the inverse, that is, A*A1=A1*A=I
%Use AX=b to get X=A-1b , you can solve a system of linear equations. It is more efficient to
use
%Determinant, rank, trace
det(A);
rank(A);
trace(A);% is equal to the sum of diagonal elements or the sum of eigenvalues, eig, so you can also judge the sum(diag(A))
%vector and matrix norms, matrix condition number
%norm(V,1) norm(V),norm(V,2) norm(V,inf)vector
%norm(A,1) norm(A) norm(A, inf) matrix
%cond(A,1) cond(A),cond(A,2) cond(A,inf)%The closer the condition number is to 1, the better the matrix performance
%Eigenvalues and eigenvectors
A=[1:3;4:6;7:9];
res1=eig(A);
[X,D]=eig(A);
[X1,D1]=eig(A, 'nobalance'); %AX=XD,A*inv(D)=I
%Eigenvalue method to solve
equation %p coefficient matrix, A=compan(p) then eig(A)==roots(p)
%Sparse matrix
%Complete storage mode and sparse storage mode, the latter stores (row,col) num in columns; there are two concepts
% One refers to the fact that the matrix has more 0 elements and has the characteristics of a sparse matrix; the other is Refers to storing
S=sparse(A) in the form of a sparse matrix; % is completely converted to sparse. When A is sparse, it is equivalent to S=A assignment call. An undefined vector
sparse(m,n);%Generate a sparse matrix
sparse(u,v,S) in which all elements of mxn are 0; %Equal-length vectors, row labels, column labels, and non-0 elements correspond to the sparse matrix
[u,v,S]=find(A );%Find the position of the non-zero elements in the matrix A (row,col) num
full(A);%Return the complete storage method corresponding to the sparse matrix A
spconvert(A) %Sparse matrix is converted into a sparse storage matrix , where A is composed of mx3 or mx4 matrix (row,col) num
spdiags(B,d,row , col);%The original matrix ]=spdiags(A);B=spdiags(A,d);E=spdiags(B,d,A);
speye(row,col); %Sparse storage unit matrix
% Two sparse operations are not sparse yet, the others are fully stored