###################################################
problem: Generating, manipulating matrix
18.4.27
How to generate matrices matrix,, and its related operations???
Solution:
D = d = Matrix (Nrow = 3,ncol = 3) #生成一个空矩阵, then fill in the contents.
A1 = C (1,4,2); A2 = C (2,2,3); A3 = C (3,1,0);
d[,1] = A1; d[,2] = A2; d[,3] = A3
D[1,] = A1; D[2,] = A2; D[3,] = A3; #D and D are transpose each other, proving that the vectors are not divided into columns.
DD = C (A1,A2,A3); Length (DD) #$ generates a vector, which is the total length of three vectors
MAT1 = Matrix (nrow = 8,ncol = 4); #得到8x4的矩阵, all elements are NA
A_mat = Matrix (Data=a,nrow=4,byrow = T); A_mat #用向量 A generation matrix, the length of the loop is not enough to complement.
MAT2 = As.matrix (Cbind (C1,C2,C3,C4)) # As.matrix, Is.matrix useful
#相关操作:
Colnames (MAT1) = C ("* *", "* *", "* *", "* *"); #设置已有矩阵的列名字: Colnames (MAT1) View column names
Rownames (MAT1) = C (); #设置行名, Rownames (mat1) View row name
MAT1[I,J];
Mat1[,c (2,4)];
mat1["name",]; #返回名为 the row for "name"
#矩阵特有运算: Transpose: T (MAT1); Matrix multiplication: mat1%*% mat2; Inversion: Solve (MAT1);
#函数rowSums (MAT1), Colsums (MAT1), Rowmeans (MAT1), Colmeans (MAT1) can calculate the row sum, row mean, column sum, column mean respectively.
Discussion extension:
Please see also:
###################################################
problem: Generating, manipulating array
18.4.27
How to generate an array of arrays,, and their related operations???
Solution:
A<-array (Rep (1:3,each=3), Dim=c (3,3))
The array function is an array () with the syntax: Array (DADTA, dim), where data must be of the same type, and dim is a vector of the lengths of each dimension.
Arrays: one-dimensional data is a vector, two-dimensional data is a matrix, an array is a direct generalization of vectors and matrices, is composed of three-dimensional or more than three-dimensional data.
Discussion extension:
Please see also:
R:matrix & array generation, Operation Matrix, array: