R Matrix
R language provides a matrix type 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.
An M x N matrix is a rectangular array of **M (row) rows** and **N columns (column)** elements.
!(#)
The following is a 2-row, 3-column matrix consisting of 6 numeric elements:
!(https://static.jyshare.com/images/mix/61f786996bcfb75972dd77712c90122bc8765269.svg)
R language matrices can be created using the matrix() function, with the following syntax:
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:
## Example
# byrow is TRUE, elements are arranged by row
M <-matrix(c(3:14), nrow=4, byrow = TRUE)
print(M)
# byrow is FALSE, elements are arranged by column
N <-matrix(c(3:14), nrow=4, byrow = FALSE)
print(N)
# Define row and column 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)
Execute the above code and the output 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
### Transpose Matrix
R language matrix provides the t() function, which can realize the row and column interchange of a matrix.
For example, if there is an m-row n-column matrix, using the t() function can convert it to an n-row m-column matrix.
!(#)
!(https://static.jyshare.com/images/mix/4a92835c45d5cd15dd00a8d90c14bdb4b8150ef0.svg)
!(https://static.jyshare.com/images/mix/8a1e10800e0d0e3ffa90917139532cfb8348fc63.svg)
## 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 3-row 2-column matrix
print(t(M))
Execute the above code and the output is:
[,1] [,2] [,3][1,] 2 6 5[2,] 1 10 4 "-----Transpose-----" [,1] [,2][1,] 2 1[2,] 6 10[3,] 5 4
### Access 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 row and column names
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 first row third column
print(P[1,3])
# Get the element at fourth row second column
print(P[4,2])
# Get the second row
print(P[2,])
# Get the third column
print(P[,3])
Execute the above code and the output is:
col1 col2 col3 row1 3 4 5 row2 6 7 8 row3 9 10 11 row4 12 13 14 5 13 col1 col2 col3 6 7 8 row1 row2 row3 row4 5 8 11 14
### Matrix Calculation
Matrices of the same size (same number of rows and columns) can be added and subtracted with each other, specifically adding and subtracting elements at each position. Matrix multiplication is more complicated. Two matrices can be multiplied if and only if the number of columns in the first matrix equals the number of rows in the second matrix.
### Matrix Addition and Subtraction
## Example
# 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)
# Add two matrices
result <- matrix1 + matrix2
cat("Addition result: ","n")
print(result)
# Subtract two matrices
result <- matrix1 - matrix2
cat("Subtraction result: ","n")
print(result)
Execute the above code and the
YouTip