Professional Documents
Culture Documents
Matrices
AUTHOR
Dr. Mohammad Nasir Abdullah
Matrices
A matrix is a two-dimensional data structure where data is arranged in rows and columns. Each
element of a matrix can be referred to by its row and column number. Matrices are particularly useful
in linear algebra operations and statistical modeling.
Arguments:
1. data : The actual data that will fill the matrix. It can be a vector of numbers, strings, or logical
values.
2. nrow : The number of rows the matrix should have.
3. ncol : The number of columns the matrix should have.
4. byrow : A logical value indicating whether the matrix should be filled by rows. If FALSE (the
default), the matrix is filled by columns.
5. dimnames : A list of two components giving the row and column names respectively.
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
#creating 2 by 3 matrix
matrix(1:6, nrow=2, ncol=3)
#creating 6 by 1 matrix
matrix(1:6, nrow=6, ncol=1)
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
[5,] 5
[6,] 6
#creating 1 by 6 matrix
matrix(1:6, nrow=1, ncol=6)
Addition
[,1] [,2]
[1,] 1 2
[2,] 3 4
[,1] [,2]
[1,] 1 3
[2,] 2 4
A + B
[,1] [,2]
[1,] 2 5
[2,] 5 8
Subtraction
A - B
[,1] [,2]
[1,] 0 -1
[2,] 1 0
Matrix Multiplication
Element-wise multiplication
A * B
[,1] [,2]
[1,] 1 6
[2,] 6 16
Matrix multiplication
A %*% B
[,1] [,2]
[1,] 5 11
[2,] 11 25
Scalar Operations
https://sta334.s3.ap-southeast-1.amazonaw s.com/day3/Matrix+Array+Data+Frame.html 3/10
10/26/23, 10:53 AM Matrices
Multiplication
A * 2
[,1] [,2]
[1,] 2 4
[2,] 6 8
Division
A/2
[,1] [,2]
[1,] 0.5 1
[2,] 1.5 2
Transpose of a Matrix
t(A)
[,1] [,2]
[1,] 1 3
[2,] 2 4
Matrix Inversion
If a matrix is invertible, you can find its inverse using the solve function.
To Inverse a matrix, it must be from square matrix and the determinant of a matrix must not be zero
[,1] [,2]
[1,] 4 2
[2,] 7 6
#checking determinant
det(I)
[1] 10
#inverse matrix
solve(I)
[,1] [,2]
[1,] 0.6 -0.2
[2,] -0.7 0.4
Diagonal of a Matrix
diag(A)
[1] 1 4
Matrix power
You can raise a matrix to a power using %^% operator from expm package
expm
A %^% 2
[,1] [,2]
[1,] 7 10
[2,] 15 22
Matrix Indices
Accessing and manipulating the data within matrices often requires understanding matrix indexing,
which lets you retrieve or modify specific data points, rows, columns, or subsections of the matrix.
Basic indexing:
1) Single element access: To access a specific element in a matrix, you use the row and column index.
2) Row access: To access a specific row, specify the row index and leave the column index blank.
Syntax: matrix[row, ]
3) Column access: To access a specific column, leave the row index blank and specify the column
index.
Advanced Indexing:
1) Multiple element access: You can access multiple elements by providing vectors for row and column
indices.
2) Conditional indexing: By using logical conditions, you can access elements that satisfy certain
criteria.
3) Negative indexing: If you provide negative indices, R will return all rows or column excluding those
indices.
Example:
Consider the following matrix:
M[2,3]
[1] 8
M[1, ]
[1] 1 4 7
M[ , 2]
[1] 4 5 6
4. Access multiple elements (eg: 1st and 3rd rows of the 2nd and 3rd columns
M[c(1,3), c(2,3)]
[,1] [,2]
[1,] 4 7
[2,] 6 9
M[ M > 6]
[1] 7 8 9
M[ -2 , ]
Exercise:
1. Create a matrix with numbers from 1 to 12, having 4 rows. Print the matrix.
2. Now, reshape the same data into a matrix with 3 columns. Print the result.
1. Create a matrix with numbers from 1 to 6, having 2 rows, filled by columns. Print the matrix.
2. Create another matrix with the same data, but this time filled by rows. Print the matrix and
compare with the previous one.
2. Assign row names as “R1”, “R2”, and “R3”. Assign column names as “C1”, “C2”, and “C3”.
mat <- matrix(1:9, nrow=3, dimnames=list(c("R1", "R2", "R3"), c("C1", "C2", "C3")))
a. Print the element in the second row and third column. b. Print the entire second row. c. Print the
entire first column.
a. Perform and print the matrix addition of A and B. b. Perform and print the matrix multiplication of A
and B.
1. Create a diagonal matrix with the numbers 4, 5, and 6 on its diagonal. Print the matrix.
2. Check if the matrix from the previous step is symmetric. Print the result.
[,1] [,2]
[1,] 2 3
[2,] 4 5
[,1] [,2]
[1,] 1 2
[2,] 3 4
a. Multiply the matrix by the scalar value 3 and print the result.
b. Divide the matrix by the scalar value 2 and print the result.
b. Find and print the inverse of matrix A. (Ensure the matrix is invertible first)
1. Create a matrix C
b. Extract the second row and third column of matrix C and print them.
Matrix M:
matrix(1:9, nrow=3,ncol=3)
a. Access and print the element in the 3rd row, 1st column.
a. Access and print the elements in the 1st and 3rd rows of the 2nd column.
b. Extract a submatrix containing the 1st and 2nd rows of the 1st and 3rd columns.
c. Can you retrieve all elements of M that are greater than 4? If so, which elements satisfy this
condition?
b. Retrieve all elements that are less than 7 and are odd numbers.
Imagine matrix M represents scores of 3 students in 3 subjects. Rows represent students and columns
represent subjects.
b. The second student retook the exam for the 1st subject and scored a 5. Update the matrix to reflect
this score.
c. Calculate the average score for the 1st student across all subjects.