R Matrix

R language provides matrix types for linear algebra research. This data structure is very similar to two-dimensional arrays in other languages, but R provides language-level matrix operation support.

Elements in a matrix can be numbers, symbols, or mathematical expressions.

AM x Nmatrix is composed ofM (row) rowsandN (column) columnselements arranged in a rectangular array.

The following is a 2-row, 3-column matrix consisting of 6 numeric elements:

Matrices in R language 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:

  • dataVector, the data of the matrix

  • nrowNumber of rows

  • ncolNumber of columns

  • byrowLogical value. If FALSE, arrange by column; if TRUE, arrange by row

  • dimnameSet the names of rows and columns

Create a numeric matrix:

Example

# If byrow is TRUE, elements are arranged by row
M <- matrix(c(3:14), nrow = 4, byrow = TRUE)
print(M)

# If byrow 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)

Executing the above code produces the following result:

[,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

Matrix Transpose

R language matrices providet()function, which can realize the row-column interchange of matrices.

For example, an m-row, n-column matrix can be converted into an n-row, m-column matrix using the t() function.

Example

# 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 a 3-row, 2-column matrix
print(t(M))

Executing the above code produces the following result:

     [,1] [,2] [,3]
[1,]    2    6    5
[2,]    1   10    4
[1] "-----转换-----"
     [,1] [,2]
[1,]    2    1
[2,]    6   10
[3,]    5    4

Accessing Matrix Elements

If you want to get matrix elements, you can use the column index and row index of the elements, similar to coordinate form.

Example

# Define the names of rows and columns
rownames = c("row1", "row2", "row3", "row4")
colnames = c("col1", "col2", "col3")

# Create a 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])

Executing the above code produces the following result:

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 

Matrix Calculations

Matrices of the same size (same number of rows and columns) can be added to or subtracted from each other, specifically by adding or subtracting the elements at each position. Matrix multiplication is more complex. Two matrices can be multiplied if and only if the number of columns of the first matrix equals the number of rows of the second matrix.

Matrix Addition and Subtraction

Example

# Create a 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)

# Add two matrices
result <- matrix1 + matrix2
cat("Addition result:","\n")
print(result)

# Subtract two matrices
result <- matrix1 - matrix2
cat("Subtraction result:","\n")
print(result)

Executing the above code produces the following result:

[,1] [,2] [,3]
[1,]    7   -1    2
[2,]    9    4    3
     [,1] [,2] [,3]
[1,]    6    0    3
[2,]    1    9    2
相加结果: 
     [,1] [,2] [,3]
[1,]   13   -1    5
[2,]   10   13    5
相减结果: 
     [,1] [,2] [,3]
[1,]    1   -1   -1
[2,]    8   -5    1

Matrix Multiplication and Division

Example

# Create a 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)

# Multiply two matrices
result <- matrix1 * matrix2
cat("Multiplication result:","\n")
print(result)

# Divide two matrices
result <- matrix1 / matrix2
cat("Division result:","\n")
print(result)

Executing the above code produces the following result:

[,1] [,2] [,3]
[1,]    7   -1    2
[2,]    9    4    3
     [,1] [,2] [,3]
[1,]    6    0    3
[2,]    1    9    2
相乘结果: 
     [,1] [,2] [,3]
[1,]   42    0    6
[2,]    9   36    6
相除结果: 
         [,1]      [,2]      [,3]
[1,] 1.166667      -Inf 0.6666667
[2,] 9.000000 0.4444444 1.5000000
Other Extensions