English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
R language provides matrix types for the study of linear algebra, which is very similar to two-dimensional arrays in other languages, but R provides language-level matrix operation support.
The elements in the matrix can be numbers, symbols, or mathematical expressions.
A M x N matrix is a rectangular array composed of M (row) rows and N columns (column)elements.
The following is a rectangular array composed of 6 A number of elements composed of 2 Row 3 Column matrix:
R language matrix can be created using the matrix() function, the syntax format is as follows:
matrix(data = NA, nrow = 1,ncol = 1,byrow = FALSE, dimnames = NULL
Parameter description:
data Vector, the data of the matrix
nrow Number of rows
ncol Number of columns
byrow Logical value, FALSE arranges by column, TRUE arranges by row
dimname Set the names of rows and columns
Create a numeric matrix:
# byrow is TRUE, elements are arranged by row M <- matrix(c(3:14), nrow = 4,byrow = TRUE print(M) # Ebyrow is FALSE, elements are arranged by column N <- matrix(c(3:14), nrow = 4,byrow = FALSE print(N) # Define the names of rows and columns 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)
The output result of executing the above code is:
[1] [2] [3] [1,] 3 4 5 [2,] 6 7 8 [3,] 9 10 11 [4,] 12 13 14 [1] [2] [3] [1,] 3 7 11 [2,] 4 8 12 [3,] 5 9 13 [4,] 6 10 14 col1 col2 col3 row1 3 4 5 row2 6 7 8 row3 9 10 11 row4 12 13 14
R language matrix provides the t() function to switch the rows and columns of a matrix.
For example, a matrix with m rows and n columns can be converted to an n row m column matrix using the t() function.
# Create a 2 Row 3 Column matrix M = matrix(c(2,6,5,1,10,4), nrow = 2,ncol = 3,byrow = TRUE print(M) [1] [2] [3] [1,] 2 6 5 [2,] 1 10 4 # Convert to 3 Row 2 Column matrix print(t(M))
The output result of executing the above code is:
[1] [2] [3] [1,] 2 6 5 [2,] 1 10 4 [1-----Conversion-----" [1] [2] [1,] 2 1 [2,] 6 10 [3,] 5 4
If you want to get matrix elements, you can use the column index and row index of the element, similar to a coordinate form.
# Define the names of rows and columns rownames = c("row1", "row2", "row3", "row4") colnames = c("col1", "col2", "col3") # Create matrix P <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(rownames, colnames)) print(P) # Get the element at the first row and third column print(P[1,3)) # Get the element at the fourth row and second column print(P[4,2)) # Get the second row print(P[2,]) # Get the third column print(P[,3))
The output result of executing the above code is:
col1 col2 col3 row1 3 4 5 row2 6 7 8 row3 9 10 11 row4 12 13 14 [1] 5 [1] 13 col1 col2 col3 6 7 8 row1 row2 row3 row4 5 8 11 14
Matrices of the same size (same number of rows and columns) can be added or subtracted, which means performing addition or subtraction on each element at the same position. The matrix multiplication is more complex. Two matrices can be multiplied if and only if the number of columns in the first matrix is equal to the number of rows in the second matrix.
# Create 2 Row 3 Column matrix matrix1 <- matrix(c(7, 9, -1, 4, 2, 3), nrow = 2) print(matrix1) matrix2 <- matrix(c(6, 1, 0, 9, 3, 2), nrow = 2) print(matrix2) # Two matrices addition result <- matrix1 + matrix2 cat("Addition result:","\n") print(result) # Two matrices subtraction result <- matrix1 - matrix2 cat("Subtraction result:","\n") print(result)
The output result of executing the above code is:
[1] [2] [3] [1,] 7 -1 2 [2,] 9 4 3 [1] [2] [3] [1,] 6 0 3 [2,] 1 9 2 Addition result: [1] [2] [3] [1,] 13 -1 5 [2,] 10 13 5 Subtraction result: [1] [2] [3] [1,] 1 -1 -1 [2,] 8 -5 1
# Create 2 Row 3 Column matrix matrix1 <- matrix(c(7, 9, -1, 4, 2, 3), nrow = 2) print(matrix1) matrix2 <- matrix(c(6, 1, 0, 9, 3, 2), nrow = 2) print(matrix2) # Two matrices multiplication result <- matrix1 * matrix2 cat("Multiplication result:","\n") print(result) # Two matrices division result <- matrix1 / matrix2 cat("Division result:","\n") print(result)
The output result of executing the above code is:
[1] [2] [3] [1,] 7 -1 2 [2,] 9 4 3 [1] [2] [3] [1,] 6 0 3 [2,] 1 9 2 Multiplication result: [1] [2] [3] [1,] 42 0 6 [2,] 9 36 6 Division result: [1] [2] [3] [1,] 1.166667 -Inf 0.6666667 [2,] 9.000000 0.4444444 1.5000000