A matrix is an R object which is similar to a vector, arranged in a two-dimensional rectangular representation. It has some additional attributes with it. All these attributes can be accessed by using the attribute()
method i.e., dimensions. The dim()
method returns the dimensionality (rows & columns) of the argument value, i.e., matrix.
Matrices can be of any type, however we mostly deal with numerical data for different computations (data analysis). Primarily, we use matrices with numerical values in R, as it is a language specialized for data analysis.
matrix(data, nrow, ncol, byrow, dimnames)
data
: is the input vector that are data attributes in the matrix.nrow
: number of rows in the matrix.ncol
: number of columns in the matrix.byrow
: is a logical type. If it is TRUE
, then the input attributes are arranged by row.dimname
: is the names that are assigned to the rows and columns.This code snippet below is generating two matrices, M
and N
(line 2 and 9). Each matrix has four rows nrow = 4
and data values from 3 to 14 c(3:14)
. It will generate a 4x3 matrix, because we have row = 4
and there is a total of 12 values between 3 and 14.
In order to set row and column names, you can use the code below on lines 9
and 10
. These lines will help to change the names of rows and columns.
# Elements are arranged by rowM <- matrix(c(3:14), nrow = 4, byrow = TRUE)# c(3:14) meas we have values in matrix# 3,4,5,6,7,8,9,10,11,12,13,14cat("Matrix M",sep =" \n ")print(M)# Give the column and row namesrownames = c ("row1", "row2", "row3", "row4")colnames = c ("col1", "col2", "col3")# c(3:14) meas we have values in matrix# 3,4,5,6,7,8,9,10,11,12,13,14N <- matrix (c (3:14), nrow = 4, byrow = TRUE, dimnames = list (rownames, colnames))cat("Matrix P",sep =" \n ")print(N)
To access elements of a matrix, we can use indexing [,]
. We can mention row number and column number to access them randomly.
It results in the 2nd
row.
It results in the 3rd
column.
It results in the element at row= 1
and col= 2
index.
This will print the whole matrix.
# Give the column and row namesrownames = c ("row1", "row2", "row3", "row4")colnames = c ("col1", "col2", "col3")# Create the matrixP <- matrix (c (3:14), nrow = 4, byrow = TRUE, dimnames = list (rownames, colnames))# Accessing the elements at 2nd Rowcat("Case#1:",sep =" \n ")print (P [2,])# Accessing the elements at 3rd columncat("Case#2:",sep =" \n ")print (P [,3])# Accessing the element at 1st row and 2nd col.cat("Case#3:",sep =" \n ")print (P [1,2])# Access whole matrix.cat("Case#4:",sep =" \n ")print (P [,])
Multiple mathematical operations can be performed on the matrices and the result is also a matrix. The dimensions (rows by columns) should be exactly identical for the matrices that are used in the operation.
This code demonstrates the addition of matrices using R as the programming language. The same rule of linear algebra will apply. Two matrices are confirmable for addition or subtraction when they have same order. matrix_A
and matrix_B
have the same dimensions So, on lines 9 and 13, they are being added and subtracted respectively.
# Create two 2 by 3 matricesmatrix_A <- matrix (c (3, 9, -1, 4, 2, 6), nrow = 2)cat ("Matrix-A","\n")print(matrix_A)matrix_B <- matrix (c (5, 2, 0, 9, 3, 4), nrow = 2)cat ("Matrix-B","\n")print(matrix_B)# Add the matricesout <- matrix_A + matrix_Bcat ("Matrices Addition is ","\n")print(out)# Difference the matricesout <- matrix_A - matrix_Bcat ("Matrices Subtraction is: ","\n")print(out)
On highlighted lines, index-by-index multiplication and division is occurring between Matrix-A
and Matrix-B
. During the division process, divide by zero case
can occur. So, such index will be replaced by -Inf
.
# Create two 2 by 3 matricesmatrix_A <- matrix (c (3, 9, -1, 4, 2, 6), nrow = 2)cat ("Matrix-A","\n")print(matrix_A)matrix_B <- matrix (c (5, 2, 0, 9, 3, 4), nrow = 2)cat ("Matrix-B","\n")print(matrix_B)# Multiply the matricesout <- matrix_A * matrix_Bcat ("Multiplication:","\n")print(out)# Divide the matricesout <- matrix_A / matrix_Bcat ("Division:","\n")print(out)