Syntax
The basic syntax for creating a matrix in R is −
- matrix(data, nrow, ncol, byrow, dimnames)
Following is the description of the parameters used −
- data is the input vector which becomes the data elements of the matrix.
- nrow is the number of rows to be created.
- ncol is the number of columns to be created.
- byrow is a logical clue. If TRUE then the input vector elements are arranged by row.
- dimname is the names assigned to the rows and columns.
In [2]:
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
In [3]:
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
In [5]:
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
In [6]:
# Access the element at 3rd column and 1st row.
print(P[1,3])
# Access the element at 2nd column and 4th row.
print(P[4,2])
# Access only the 2nd row.
print(P[2,])
# Access only the 3rd column.
print(P[,3])
[1] 5 [1] 13 col1 col2 col3 6 7 8 row1 row2 row3 row4 5 8 11 14
In [11]:
# Create two 2x3 matrices.
matrix1 = matrix(c(3, 9, -1, 4, 2, 6), nrow = 2)
print(matrix1)
matrix2 = matrix(c(5, 2, 0, 9, 3, 4), nrow = 2)
print(matrix2)
# Add the matrices.
result = matrix1 + matrix2
cat("\nResult of addition","\n")
print(result)
# Subtract the matrices
result = matrix1 - matrix2
cat("\nResult of subtraction","\n")
print(result)
[,1] [,2] [,3] [1,] 3 -1 2 [2,] 9 4 6 [,1] [,2] [,3] [1,] 5 0 3 [2,] 2 9 4 Result of addition [,1] [,2] [,3] [1,] 8 -1 5 [2,] 11 13 10 Result of subtraction [,1] [,2] [,3] [1,] -2 -1 -1 [2,] 7 -5 2
In [12]:
# Multiply the matrices.
result = matrix1 * matrix2
cat("Result of multiplication","\n")
print(result)
# Divide the matrices
result = matrix1 / matrix2
cat("Result of division","\n")
print(result)
Result of multiplication [,1] [,2] [,3] [1,] 15 0 6 [2,] 18 36 24 Result of division [,1] [,2] [,3] [1,] 0.6 -Inf 0.6666667 [2,] 4.5 0.4444444 1.5000000
In [ ]: