Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Practical 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Matrix Computations

Various mathematical operations are performed


on the matrices using the R operators. The result
of the operation is also a matrix.
The dimensions (number of rows and columns)
should be same for the matrices involved in the
operation.
Matrix Addition & Subtraction
Live Demo
# 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("Result of addition","\n")
print(result)

# Subtract the matrices


result <- matrix1 - matrix2
cat("Result of subtraction","\n")
print(result)
When we execute the above code, it produces the
following 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
Matrix Multiplication & Division
Live Demo
# 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)

# 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)
When we execute the above code, it produces the
following 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 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

Equation for Inverse of Matrix:

There are two ways in which the inverse of a Matrix


can be found:
 Using the solve() function:

solve() is a generic built-in function in R which is


helpful for solving the following linear algebraic
equation just as shown above in the image. It can be
applied both on vectors as well as a matrix.

# R program to find inverse of a Matrix


  
# Create 3 different vectors
# using combine method.
a1 <- c(3, 2, 5)
a2 <- c(2, 3, 2)
a3 <- c(5, 2, 4)
  
# bind the three vectors into a matrix 
# using rbind() which is basically
# row-wise binding.
A <- rbind(a1, a2, a3)
  
# print the original matrix
print(A)
  
# Use the solve() function 
# to calculate the inverse.
T1 <- solve(A)
  
# print the inverse of the matrix.
print(T1)
Output:
[,1] [,2] [,3]
a1 3 2 5
a2 2 3 2
a3 5 2 4

a1 a2 a3
[1,] -0.29629630 -0.07407407 0.4074074
[2,] -0.07407407 0.48148148 -0.1481481
[3,] 0.40740741 -0.14814815 -0.1851852

 Using the inv() function:


inv() function is a built-in function in R which is
especially used to find the inverse of a matrix.
Note:Ensure that you have installed the ‘matlib’
package in your environment.
Finding Determinant of Matrix:
# Create 3 different vectors.
a1 <- c(3, 2, 8)
a2 <- c(6, 3, 2)
a3 <- c(5, 2, 4)
  
# Bind the 3 matrices row-wise 
# using the rbind() function.
A <- rbind(a1, a2, a3)
  
# determinant of matrix
print(det(A))
Output:
-28
Finding Inverse of Matrix:
# find inverse using the inv() function.
print(inv(t(A))) 
Output:
[1,] -0.2857143 0.5 0.1071429
[2,] -0.2857143 1.0 -0.1428571
[3,] 0.7142857 -1.5 0.1071429

Example:
A group took a trip on a bus, at $3 per child and $3.20
per adult for a total of $118.40. They took
the train back at $3.50 per child and $3.60 per adult for
a total of $135.20.
How many children, and how many adults?
First, let us set up the matrices (be careful to get the
rows and columns correct!):

This is just like the example above:


XA = B
So to solve it we need the inverse of "A":

 
Now we have the inverse we can solve using:
X = BA-1
There were 16 children and 22 adults!
The answer almost appears like magic. But it is based
on good mathematics.
Calculations like that (but using much larger
matrices) help Engineers design buildings,
are used in video games and computer
animations to make things look 3-
dimensional, and many other places.
Matrix Transpose in R
Transpose of a matrix is an operation in which we
convert the rows of the matrix in column and column of
the matrix in rows. The general equation for performing
the transpose of a matrix is as follows.
Aij = Aji  where i is not equal to j
Example:
Matrix M ---> [1, 8, 9
12, 6, 2
19, 42, 3]

Transpose of M
Output ---> [1, 12, 19
8, 6, 42,
9, 2, 3]
Transpose of a Matrix can be performed in two ways:
 Finding the transpose by using the t()
function
filter_none
brightness_4
# R program for Transpose of a Matrix
  
# create a matrix with 2 rows 
# using matrix() method
M <- matrix(1:6, nrow = 2) 
  
# print the original matrix
print(M)
  
# transpose of matrix
# using t() function.
t <- t(M) 
  
# print the transpose matrix
print(t)
Output:
[, 1] [, 2] [, 3]
[1, ] 1 3 5
[2, ] 2 4 6

[, 1] [, 2]
[1, ] 1 2
[2, ] 3 4
[3, ] 5 6

You might also like