本节讲“矩阵”,“数组”
> M <- matrix(c(3:14),nrow=4,byrow=TRUE)
> print(M)
[,1] [,2] [,3]
[1,] 3 4 5
[2,] 6 7 8
[3,] 9 10 11
[4,] 12 13 14
> M <- matrix(c(3:14),nrow=4,byrow=FALSE)
> print(M)
[,1] [,2] [,3]
[1,] 3 7 11
[2,] 4 8 12
[3,] 5 9 13
[4,] 6 10 14
> N <- matrix(c(3:14),nrow=4,byrow=FALSE)
> print(N)
[,1] [,2] [,3]
[1,] 3 7 11
[2,] 4 8 12
[3,] 5 9 13
[4,] 6 10 14
> #define the column and row names
> rownames = c("row1","row2","row3","row4")
> colnames = c("col1","col2","col3")
> p <- matrix(c(3:14),nrow = 4, byrow = TRUE, dimnames = list(rownames,colnames))
> print(p)
col1 col2 col3
row1 3 4 5
row2 6 7 8
row3 9 10 11
row4 12 13 14
> #访问矩阵的元素
> # Access the element at 3rd column and 1st row.
> P <- matrix(c(3:14),nrow = 4, byrow = TRUE, dimnames = list(rownames,colnames))
> print(P)
col1 col2 col3
row1 3 4 5
row2 6 7 8
row3 9 10 11
row4 12 13 14
>
> # Access the element at 3rd column and 1st row.
> print(P[1,3])
[1] 5
>
> # Access the element at 2nd column and 4th row.
> print(P[4,2])
[1] 13
>
> # Access only the 2nd row.
> print(P[2,])
col1 col2 col3
6 7 8
>
> # Access only the 3rd column.
> print(P[,3])
row1 row2 row3 row4
5 8 11 14
> #矩阵加法和减法
> # Create two 2x3 matrices.
> matrix1 <- matrix(c(3, 9, -1, 4, 2, 6), nrow = 2)
> print(matrix1)
[,1] [,2] [,3]
[1,] 3 -1 2
[2,] 9 4 6
>
> matrix2 <- matrix(c(5, 2, 0, 9, 3, 4), nrow = 2)
> print(matrix2)
[,1] [,2] [,3]
[1,] 5 0 3
[2,] 2 9 4
>
> # Add the matrices.
> result <- matrix1 + matrix2
> cat("Result of addition","
+ ")
Result of addition
> print(result)
[,1] [,2] [,3]
[1,] 8 -1 5
[2,] 11 13 10
>
> # Subtract the matrices
> result <- matrix1 - matrix2
> cat("Result of subtraction","
+ ")
Result of subtraction
> print(result)
[,1] [,2] [,3]
[1,] -2 -1 -1
[2,] 7 -5 2
> #矩阵乘法和除法
> # Create two 2x3 matrices.
> matrix1 <- matrix(c(3, 9, -1, 4, 2, 6), nrow = 2)
> print(matrix1)
[,1] [,2] [,3]
[1,] 3 -1 2
[2,] 9 4 6
>
> matrix2 <- matrix(c(5, 2, 0, 9, 3, 4), nrow = 2)
> print(matrix2)
[,1] [,2] [,3]
[1,] 5 0 3
[2,] 2 9 4
>
> # Multiply the matrices.
> result <- matrix1 * matrix2
> cat("Result of multiplication","
+ ")
Result of multiplication
> print(result)
[,1] [,2] [,3]
[1,] 15 0 6
[2,] 18 36 24
>
> # Divide the matrices
> result <- matrix1 / matrix2
> cat("Result of division","
+ ")
Result of division
> print(result)
[,1] [,2] [,3]
[1,] 0.6 -Inf 0.6666667
[2,] 4.5 0.4444444 1.5000000
>
数组
> #数组·
> #create two vectors of different lengths
> vector1 <- c(5,9)
> vector2 <- c(10,11,12,13,14,15)
>
> result <- array(c(vector1,vector2),dim = c(3,3,2))
> print(result)
, , 1
[,1] [,2] [,3]
[1,] 5 11 14
[2,] 9 12 15
[3,] 10 13 5
, , 2
[,1] [,2] [,3]
[1,] 9 12 15
[2,] 10 13 5
[3,] 11 14 9
> vector1 <- c(5)
> vector2 <- c(10,11,12,13,14,15)
> result <- array(c(vector1,vector2),dim = c(3,3,2))
> print(result)
, , 1
[,1] [,2] [,3]
[1,] 5 12 15
[2,] 10 13 5
[3,] 11 14 10
, , 2
[,1] [,2] [,3]
[1,] 11 14 10
[2,] 12 15 11
[3,] 13 5 12
> # Create two vectors of different lengths.
> vector1 <- c(5,9,3)
> vector2 <- c(10,11,12,13,14,15)
>
> # Take these vectors as input to the array.
> result <- array(c(vector1,vector2),dim = c(3,3,2))
> print(result)
, , 1
[,1] [,2] [,3]
[1,] 5 10 13
[2,] 9 11 14
[3,] 3 12 15
, , 2
[,1] [,2] [,3]
[1,] 5 10 13
[2,] 9 11 14
[3,] 3 12 15
> #命名列和行
> 我们可以使用dimnames参数给数组中的行,列和矩阵命名。> 我们可以使用dimnames参数给数组中的行,列和矩阵命名。
> #我们可以使用dimnames参数给数组中的行,列和矩阵命名。
> #create two vectors of different lengths
> vector1 <- c(5,9,3)
> vector2 <-c(10,12,13,14,15,16)
> column.names <- c("col1","col2","col3")
> row.names <- c("row1","row2","row3")
> matrix.names <- c("Matrix1","Matrix2")
>
> #Take these vectors as input to the array
> result <- array(c(vector1,vector2),dim=c(3,3,2),dimnames = list(row.names,column.names,matrix.names))
> print(result)
, , Matrix1
col1 col2 col3
row1 5 10 14
row2 9 12 15
row3 3 13 16
, , Matrix2
col1 col2 col3
row1 5 10 14
row2 9 12 15
row3 3 13 16
> #访问数组元素
> # Print the third row of the second matrix of the array.
> print(result[3,,2])
col1 col2 col3
3 13 16
>
> # Print the element in the 1st row and 3rd column of the 1st matrix.
> print(result[1,3,1])
[1] 14
>
> # Print the 2nd Matrix.
> print(result[,,2])
col1 col2 col3
row1 5 10 14
row2 9 12 15
row3 3 13 16
>
> # Create two vectors of different lengths.
> vector1 <- c(5,9,3)
> vector2 <- c(10,11,12,13,14,15)
>
> # Take these vectors as input to the array.
> new.array <- array(c(vector1,vector2),dim = c(3,3,2))
> print(new.array)
, , 1
[,1] [,2] [,3]
[1,] 5 10 13
[2,] 9 11 14
[3,] 3 12 15
, , 2
[,1] [,2] [,3]
[1,] 5 10 13
[2,] 9 11 14
[3,] 3 12 15
> # Use apply to calculate the sum of the rows across all the matrices.
> result <- apply(new.array, c(1), sum)
> print(result)
[1] 56 68 60
> # Use apply to calculate the sum of the columns across all the matrices.
> result <- apply(new.array, c(2),sum)
> print(result)
[1] 34 66 84
>
|
请发表评论