【D1 D2】2*1
【T1 T2】1*2
Requires random changes of D1 and D2, and the matrix is multiplied 100 times
rm(list=ls()) gc() options(scipen = 2000) ###########################################3############################### Define TT matrix (1*2)TT <- matrix(c(1,3),1,2) DD<- matrix(c(1,2),2,1) result1 <- DD %*% TT m1=result1 ############################################### Set the random range of integersx <- 1:100 m=() ############################ Transform DD Matrix (2*1)####################for (i in 2:100){ D2<- matrix(c(sample(x,1,replace=TRUE),sample(x,1,replace=TRUE)),2,1) # print(D2) result <- D2%*% TT print(result) m <- rbind(m,result) result1 <- result %*% result1 } (finally_result <- result1) (m_all <- rbind(m,m1))
Supplement: Matrix operations and operations in R language
1. Transpose operation
For matrix A, function t(A) represents the transposition of matrix A, such as:
> A=matrix(1:6,nrow=2); > A; [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 > t(A); [,1] [,2] [1,] 1 2 [2,] 3 4 [3,] 5 6
2. Find the determinant of the square matrix
The function det() is to find the value of the matrix determinant, such as
> det(matrix(1:4,ncol=2)); [1] -2
3. The inner product of vectors
For n-dimensional vector x, it can be regarded as an nxl-order matrix or an lxn-order matrix. If x and y are the same
A vector of dimensions, then x%*%Y means x and y are the inner product. For example,
>x=1:5; Y=2*1:5 >x%*%y [,1] [1,]110
The function crossprod() is an inner product operation function (represents the cross product), and crossprod(x,y) calculates the inner product of vectors x and y, that is, t(x) %*% y'. crossprod(x) represents the inner product of x and x.
Similarly, tcrossprod(x,y) means 'x%*%t(Y)', that is, the outer product of x and y, also known as the cross product. tcrossprod(x) means that x and x are made into an external product. For example:
> x=1:5; y=2*1:5; > crossprod(x); [,1] [1,] 55 > crossprod(x,y); [,1] [1,] 110 > tcrossprod(x); [,1] [,2] [,3] [,4] [,5] [1,] 1 2 3 4 5 [2,] 2 4 6 8 10 [3,] 3 6 9 12 15 [4,] 4 8 12 16 20 [5,] 5 10 15 20 25 > tcrossprod(x,y); [,1] [,2] [,3] [,4] [,5] [1,] 2 4 6 8 10 [2,] 4 8 12 16 20 [3,] 6 12 18 24 30 [4,] 8 16 24 32 40 [5,] 10 20 30 40 50
4. The outer product of vector (cross product)
Assuming x and y are n-dimensional vectors, then x%o%y means x and y are the outer product. For example,
> x%o%y; [,1] [,2] [,3] [,4] [,5] [1,] 2 4 6 8 10 [2,] 4 8 12 16 20 [3,] 6 12 18 24 30 [4,] 8 16 24 32 40 [5,] 10 20 30 40 50
outer() is a more powerful external product operation function. outer(x,y) calculates the outer product of vector two and y, which is equivalent to the x %o%y function.
The general calling format of outer() is outer(x, y, fun=”*”)
Where x, y matrix (or vector), fun is an external product operation function, and the default value is multiplication operation. The function outer() is very useful when drawing 3D surfaces, which generates a mesh of x and y.
5. Multiplication of matrix
Suppose A and B are two matrices. The matrix multiplication in the usual sense is done by A%*%B, and crossprod(A,B) represents
t(A)%*%B, while tcrossprod(A,B) represents A%*%t(B). Finally, we know through the operation that x%*%A%*%x is a quadratic type.
> A=array(1:9,dim=(c(3,3))) > B=array(9:1,dim=(c(3,3))) > A%*%B; [,1] [,2] [,3] [1,] 90 54 18 [2,] 114 69 24 [3,] 138 84 30 > crossprod(A,B)==t(A)%*%B; [,1] [,2] [,3] [1,] TRUE TRUE TRUE [2,] TRUE TRUE TRUE [3,] TRUE TRUE TRUE > tcrossprod(A,B)==A%*%t(B); [,1] [,2] [,3] [1,] TRUE TRUE TRUE [2,] TRUE TRUE TRUE [3,] TRUE TRUE TRUE
6. Generate diagonal matrix and matrix diagonal operation
The function diag() depends on its variable. When v is a vector, diag(v) represents a diagonal matrix with the elements of v as diagonal elements. When M is a matrix, diag(M) represents a vector of elements on the diagonal M. For example,
> v=c(1,4,5); > diag(v); [,1] [,2] [,3] [1,] 1 0 0 [2,] 0 4 0 [3,] 0 0 5 > M=array(1:9,dim=c(3,3)); > diag(M); [1] 1 5 9
7. Solve linear equation systems and find the inverse matrix of the matrix
If you solve the linear equation system Ax=b, its command form is solve(A,b), and find the inverse of matrix A, its command form is solve(A). Suppose the matrix A=t(array(c(1:8,10), dim=c(3,3))), b<-c(1,1,1), then the command to solve the solution x of the equation system Ax=b and find the inverse matrix of matrix A is as follows:
> A=t(array(c(1:8,10),dim=c(3,3))); > b=c(1,1,1); > x=solve(A,b); > x; [1] -1.000000e+00 1.000000e+00 3.806634e-16 > solve(A); [,1] [,2] [,3] [1,] -0.6666667 -1.333333 1 [2,] -0.6666667 3.666667 -2 [3,] 1.0000000 -2.000000 1
8. Find the eigenvalues and eigenvectors of the matrix
The function eigen(Sm) is to find the eigenvalue and eigenvector of the symmetric matrix Sm. The command form is: ev=eigen(Sm), then ev stores the eigenvalue and eigenvector of the symmetric matrix Sm, which is given in the form of a list, where ev$values is a vector composed of the eigenvalue of Sm, and ev$vectors is a matrix composed of the eigenvector of Sm. For example,
> Sm=crossprod(A,A); > ev=eigen(Sm); > ev; $values [1] 303.19533618 0.76590739 0.03875643 $vectors [,1] [,2] [,3] [1,] -0.4646675 0.833286355 0.2995295 [2,] -0.5537546 -0.009499485 -0.8326258 [3,] -0.6909703 -0.552759994 0.4658502
The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.