0% found this document useful (0 votes)
35 views8 pages

Matrix Codes

Uploaded by

abhiipand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views8 pages

Matrix Codes

Uploaded by

abhiipand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

## ***************************************************

## Define Matrix in R, using matrix() function


A <- matrix(c(1, 2, 3, 4), nrow=2, ncol=2, byrow = TRUE)
# Default is byrow=FALSE
> A
[,1] [,2]
[1,] 1 2
[2,] 3 4
## To enter the matrix elements columnwise, we use
A <- matrix(c(1, 2, 3, 4), nrow=2, ncol=2, byrow = FALSE)
>A
[,1] [,2]
[1,] 1 3
[2,] 2 4
## Rectangular matrix
## rows=3, columns=2
A <- matrix(c(1, 5, 3, 6, 4, 7), nrow=3, ncol=2, byrow = TRUE) # row-wise
>A
[,1] [,2]
[1,] 1 5
[2,] 3 6
[3,] 4 7

A <- matrix(c(1, 5, 3, 6, 4, 7), nrow=3, ncol=2, byrow = FALSE) # column-wise


>A
[,1] [,2]
[1,] 1 6
[2,] 5 4
[3,] 3 7
## rows=2, columns=3
A <- matrix(c(1, 5, 3, 6, 4, 7), nrow=2, ncol=3, byrow = TRUE) # row-wise
> A
[,1] [,2] [,3]
[1,] 1 5 3
[2,] 6 4 7
> B <- matrix(c(1, 5, 3, 6, 4, 7), nrow=2, ncol=3, byrow = FALSE) # column-wise
> B
[,1] [,2] [,3]
[1,] 1 3 4
[2,] 5 6 7

## *********************************************************
## Identity matrix, using diag() function
A <- diag(3) # define identity matrix of order 3x3
> A
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
## Diagonal matrix with specified elements
A <- diag(c(1,5,9)) # define diagonal matrix
> A
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 5 0
[3,] 0 0 9

## Transpose of A, using t(A)


B <- matrix(c (9 ,8 ,6 ,2 ,7 ,1) ,nrow=2 ,ncol=3, byrow=TRUE)
> B
[,1] [,2] [,3]
[1,] 9 8 6
[2,] 2 7 1
> t(B)
[,1] [,2]
[1,] 9 2
[2,] 8 7
[3,] 6 1
> A <- matrix(c (9 ,8 ,6 ,2 ,7 ,1) ,nrow=3 ,ncol=2, byrow=TRUE)
> A
[,1] [,2]
[1,] 9 8
[2,] 6 2
[3,] 7 1
> t(A)
[,1] [,2] [,3]
[1,] 9 6 7
[2,] 8 2 1
## verification transpose of transpose will give the original matrix
## Applying transpose function twice
> t(t(A))
[,1] [,2]
[1,] 9 8
[2,] 6 2
[3,] 7 1

## ADDITION and SUBTRACTION


## Define matrices A and B of same order
## Entry of Matrix A
A <- matrix(c(6, 5, 8, 7), nrow=2, ncol=2, byrow = TRUE)
> A
[,1] [,2]
[1,] 6 5
[2,] 8 7
## Entry of Matrix B
B <- matrix(c(1, 2, 3, 4), nrow=2, ncol=2, byrow = TRUE)
> B
[,1] [,2]
[1,] 1 2
[2,] 3 4
## Adding of A and B
> A + B
[,1] [,2]
[1,] 7 7
[2,] 11 11
## Subtraction
> A - B
[,1] [,2]
[1,] 5 3
[2,] 5 3
## MULTIPLICATION A of order m x n and B of order n x p
## R command is A%*%B
## Entry of Matrix A
A <- matrix(c(6, 5, 8, 7), nrow=2, ncol=2, byrow = TRUE)
## Entry of Matrix B
B <- matrix(c(1, 2, 3, 4), nrow=2, ncol=2, byrow = TRUE)
## Multiplication of A with B
A %*% B
[,1] [,2]
[1,] 21 32
[2,] 29 44
B %*% A
B %*% A
[,1] [,2]
[1,] 22 19
[2,] 50 43
## Example: rectangular matrices
A <- matrix(c (1 ,3 ,2 ,2 ,5 ,1) ,nrow=2 ,ncol=3, byrow=TRUE)
> A
[,1] [,2] [,3]
[1,] 1 3 2
[2,] 2 5 1
B <- matrix(c (2 ,1 ,3 ,1 ,5 ,2) ,nrow=3 ,ncol=2, byrow=TRUE)
> B
[,1] [,2]
[1,] 2 1
[2,] 3 1
[3,] 5 2
> A %*% B
[,1] [,2]
[1,] 21 8
[2,] 24 9
## ***********************************************************
## Computation of Determinant, using det() function
A <- matrix(c(1, 2, 3, 4), nrow=2, ncol=2, byrow = TRUE)
det(A)
[1] -2
B <- matrix(c(1, 2, 1, 3, 2, 1, 4, 5, 6), nrow=3, ncol=3, byrow = TRUE)
> det(B)
[1] -14
## ***********************************************************
## Computation of INVERSE, using solve() function
A <- matrix(c(1, 2, 3, 4), nrow=2, ncol=2, byrow = TRUE)
> solve(A)
[,1] [,2]
[1,] -2.0 1.0
[2,] 1.5 -0.5

## verification
inv_A <- solve(A)
> inv_A%*%A
[,1] [,2]
[1,] 1 4.440892e-16
[2,] 0 1.000000e+00

## ********************************************************
## Eigen values and vectors
A <- matrix(c(5, 4, 1, 2), nrow=2, ncol=2, byrow = TRUE)
> A
[,1] [,2]
[1,] 5 4
[2,] 1 2
## Eigen values
eigen(A)$values
[1] 6 1
## Eigen vectors
eigen(A)$vectors
[,1] [,2]
[1,] 0.9701425 -0.7071068
[2,] 0.2425356 0.7071068
> eigen(A) ## values & vectors
$values
[1] 6 1

$vectors
[,1] [,2]
[1,] 0.9701425 -0.7071068
[2,] 0.2425356 0.7071068
## det(A)=product of eigen values
det(A)
[1] 6
## ******************************************
## Solution of linear equations AX=b, using function solve(A, b)
## Example 1
## 2x-y+3z-8, -x+2y+z=4, 3x+y-4z=0
## Enter matrix A
A <- matrix(c(2,-1,3,-1,2,1,3,1,-4), nrow=3, ncol=3, byrow=TRUE)
> A
[,1] [,2] [,3]
[1,] 2 -1 3
[2,] -1 2 1
[3,] 3 1 -4
## Entry of b
b <- c(8,4,0)
solve(A, b)
[1] 2 2 2
## solution x=2, y=2, z=2
## --------------------------------------------
## Example 2
## 2x-y+3z=9, x+y+z=6, x-y+z=2
A <- matrix(c(2,-1,3,1,1,1,1,-1,1), nrow=3, ncol=3, byrow=TRUE)
> A
[,1] [,2] [,3]
[1,] 2 -1 3
[2,] 1 1 1
[3,] 1 -1 1
b <- c(9,6,2)
solve(A, b)
[1] 1 2 3
## solution x=1, y=2, z=3
## --------------------------------------------
## Example 3
## 2x+3y+z=9, x+2y+3z=6, x+y+2z=8
A <- matrix(c(2,3,1,1,2,3,1,1,2), nrow=3, ncol=3, byrow=TRUE)
>A
[,1] [,2] [,3]
[1,] 2 3 1
[2,] 1 2 3
[3,] 1 1 2
b <- c(9, 6, 8)
solve(A, b)
[1] 8.75 -3.25 1.25
## solution x=8.75, y=-3.25, z=1.25
## --------------------------------------------
## Example 4
## x+2y+3z=6, 2x+4y+z=7, 3x+2y+9z=4
A <- matrix(c(1,2,3,2,4,1,3,2,9), nrow=3, ncol=3, byrow=TRUE)
>A
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 2 4 1
[3,] 3 2 9
b <- c(6, 7, 4)
solve(A, b)
[1] -4.0 3.5 1.0

## solution x=-4.0, y=3.5, z=1.0


## --------------------------------------------
## Example 5
## 5x-6y+4z=15, 7x+4y-3z=1, 2x+y+6z=4
A <- matrix(c(5,-6,4,7,4,-3,2,1,6), nrow=3, ncol=3, byrow=TRUE)
b <- c(15, 1, 4)
solve(A, b)
[1] 1.0811456 -1.2553699 0.5155131
## solution x=1.0811456, y=-1.2553699, z=0.5155131
## --------------------------------------------
## Example 6
## x+y+z=6, 9x+2y+3z=10, 6x+2y+4z=1
A <- matrix(c(1,1,1,9,2,3,6,2,4), nrow=3, ncol=3, byrow=TRUE)
> A
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 9 2 3
[3,] 6 2 4
b <- c(6, 10, 1)
solve(A, b)
[1] 0.7 12.2 -6.9
## solution x=0.7, y=12.2, z=-6.9
## *********************************************
##20x+y-2z=17, 3x+20y-z=-18, 2x-3y+20z=25
# Answer x=1, y=-1, z=1
A <- matrix(c(20,1,-2,3,20,-1,2,-3,20), 3, 3, byrow=T)
b <- c(17, -18, 25)
solve(A, b)
## *********************************************************
## x+y+z=10,2 x+y+2 z=17,3 x+2 y+z=17
A <- matrix(c(1,1,2,3,1,-3,2,-3,-5), 3, 3, byrow=T)
b <- c(4, 4, -5)
solve(A, b)
## *********************************************
## x+y+z=10, 2x+y+2z=17, 3x+2y+z=17$
A <- matrix(c(1,1,1,2,1,2,3,2,1), 3, 3, byrow=T)
b <- c(10, 17, 17)
solve(A, b)
##**********************************************
A <- matrix(c(1,1,2,3,1,-3,2,-3,-5), 3, 3, byrow=T)
b <- c(4, 4, -5)
solve(A, b)
# 1.2962963 1.6666667 0.5185185

## *********************************************
## x+y+z=10, 2x+y+2z=17, 3x+2y+z=17
A <- matrix(c(1,1,1,2,1,2,3,2,1), 3, 3, byrow=T)
b <- c(10, 17, 17)
solve(A, b)
# 2 3 5
## *********************************************
## 2x+3y-z=5, 4x+4y-3z=3, 2x-3y+2z=2
A <- matrix(c(2,3,-1,4,4,-3,2,-3,2), 3, 3, byrow=T)
b <- c(5, 3, 2)
solve(A, b)
# 1 2 3
## *********************************************
## 2x+4y+z=2, 3x+2y-2z=-2, x-y+z=6
A <- matrix(c(2,4,1,3,2,-2,1,-1,1), 3, 3, byrow=T)
b <- c(2, -2, 6)
solve(A, b)
# 2.0 -1.2 2.8

## *********************************************
## 5x+2y+z=4, 7x+y-5z=8, 3x+7y-4z=10
A <- matrix(c(5,2,1,7,1,-5,3,7,-4), 3, 3, byrow=T)
b <- c(4, 8, 10)
solve(A, b)
# 0.5903084 0.8281938 -0.6079295

## *********************************************
## 2x+2y+z=12, 3x+2y+2z=8, 5x+10y-8z=10
A <- matrix(c(2,2,1,3,2,2,5,10,-8), 3, 3, byrow=T)
b <- c(12, 8, 10)
solve(A, b)
# -12.750 14.375 8.750

## *********************************************
## 2x-y=0, -x+2y-z=0, -y+2z-u=0, -z+2u=1
A <- matrix(c(2,-1,0,0,-1,2,-1,0, 0,-1,2,-1, 0,0,-1,2), 4, 4, byrow=T)
b <- c(0, 0, 0, 1)
solve(A, b)
# 0.2 0.4 0.6 0.8
## *********************************************
## 2x-3y+z=-1, x+4y+5z=25, 3x-4y+z=2
A <- matrix(c(2,-3,1,1,4,5,3,-4,1), 3, 3, byrow=T)
b <- c(-1, 25, 2)
solve(A, b)
# 8.7 5.7 -1.3
## *********************************************
## 10x+y+z=12, x+10y+z=12, x+y+10z=12
A <- matrix(c(10,1,1,1,10,1,1,1,10), 3, 3, byrow=T)
b <- c(12, 12, 12)
solve(A, b)
# 1 1 1
## *********************************************
## 5x-2y+z=4, 7x+y-5z=8, 3x+7y+4z=10
A <- matrix(c(5,-2,1,7,1,-5,3,7,4), 3, 3, byrow=T)
b <- c(4, 8, 10)
solve(A, b)
# 1.1192661 0.8685015 0.1406728
## *********************************************
## 5x-y+z=10, 2x+4y=12, x+y+5z=-1
A <- matrix(c(1,-1,1,2,4,0,1,1,5), 3, 3, byrow=T)
b <- c(10, 12, -1)
solve(A, b)
# 9.857143 -1.928571 -1.785714
## *********************************************
## 5x+2y+z=12 x+4y+2z=15 x+2y+5z=20
A <- matrix(c(5,2,1,1,4,2,1,2,5), 3, 3, byrow=T)
b <- c(12, 15, 20)
solve(A, b)
# 1 2 3
## *********************************************
## 10x+2y+z=9, 2x+20y-2z=-14, -2x+3y+10z=22
A <- matrix(c(10,2,1,1,20,-2,-2,3,10), 3, 3, byrow=T)
b <- c(9, -14, 22)
solve(A, b)
# 0.7479675 -0.4878049 2.4959350
## *********************************************
## x+y+z=3, 2x-y+3z=16, 3x+y-z=-3
A <- matrix(c(1,1,1,2,-1,3,3,1,-1), 3, 3, byrow=T)
b <- c(3, 16, -3)
solve(A, b)
# 1 -2 4
## *********************************************
## 10x+y+z=12 , 2x+10y+z=13 , 2x+2y+10z=14
A <- matrix(c(10,1,1,2,10,1,2,2,10), 3, 3, byrow=T)
b <- c(12, 13, 14)
solve(A, b)
# 1 1 1
## *********************************************
## x_1+2 x_2+3 x_3=14, 2 x_1+5 x_2+2 x_3=18, 3 x_1+x_2+5 x_3=20
A <- matrix(c(1,2,3,2,5,2,3,1,5), 3, 3, byrow=T)
b <- c(14, 18, 20)
solve(A, b)
# 1 2 3
## *********************************************
## 2x-3y+10z=3 -x+4y+2z=20 5x+2y+z=-12
A <- matrix(c(2,-3,10,-1,4,2,5,2,1), 3, 3, byrow=T)
b <- c(3, 20, -12)
solve(A, b)
# -4 3 2
## *********************************************
## x+y+2z=4, 3x+y-3z=4, 2x-3y-5z=-5
A <- matrix(c(1,1,2,3,1,-3,2,-3,-5), 3, 3, byrow=T)
b <- c(4, 4, -5)
solve(A, b)
# 1.2962963 1.6666667 0.5185185
## *********************************************
2x + y + z -2u= -10
4x + 2z + u =8
3x + 2y + 2z =7
x + 3y + 2z -u=-5
A <- matrix(c(2,1,1,-2,4,0,2,1,3,2,2,0,1,3,2,-1), 4, 4, byrow=T)
b <- c(-10, 8, 7, -5)
solve(A, b)
# 5 6 -10 8
## *********************************************
2x +y -4z +u =4
-4x + 3y + 5z-2u=-10
x -y + z -2u =2
x + y -3z + 2u=-1
A <- matrix(c(2,1,-4,1,-4,3,5,-2,1,-1,1,-2,1,3,-3,2), 4, 4, byrow=T)
b <- c(4, -10, 2, -1)
solve(A, b)
# 0.3636364 -1.2045455 -1.3409091 -0.8863636
## *********************************************
data <- c(2,-1,0,0,-1,2,-1,0,0,-1,2,-1,0,0,-1,2)
A <- matrix(data, 4, 4, byrow=T)
b <- c(0,0,0,1)
solve(A,b)
#0.2 0.4 0.6 0.8
## *********************************************
#Solve the following system of simultaneous equations using matrix methods.
a + 2b + 3c + 4d + 5e = -5
2a + 3b + 4c + 5d + e = 2
3a + 4b + 5c + d + 2e = 5
4a + 5b + c + 2d + 3e = 10
5a + b + 2c + 3d + 4e = 11

data <- c(1,2,3,4,5,2,3,4,5,1,3,4,5,1,2,4,5,1,2,3,5,1,2,3,4)


A <- matrix(data, 5, 5, byrow=T)
b <- c(-5,2,5,10,11)
solve(A,b)
# 3.5066667 0.1066667 -0.6933333 -0.2933333 -1.0933333
## *********************************************
A <- matrix(c (1 ,3 ,3 ,9 ,6 ,5) ,2 ,3)
B <- matrix(c (9 ,8 ,8 ,2 ,9 ,0) ,2 ,3)
rbind(A,B)
rbind(B,A)
cbind(A,B)
cbind(B,A)
A <- matrix(c(0.1, 0.4, 0.3, 0.2,
0.2, 0.1, 0.4, 0.3,
0.3, 0.2, 0.1, 0.4,
0.4, 0.3, 0.2, 0.1),
nrow=4, ncol=4, byrow=T)
A[ ,1]
A[1, ]
A[2,3]
det(A)
solve(A)
Ainv <- solve(A)
# verification
Ainv%*%A
zapsmall(Ainv%*%A)

b <- matrix(c(1, 2, 3, 1), nrow=4, ncol=1, byrow=F)


solve(A,b)

##**************************************************************
A <- matrix(c(13, -4, 2, -4, 11, -2, 2, -2, 8), 3, 3, byrow=TRUE)
eigen(A)
ev <- eigen(A)
# extract components
L <- ev$values
V <- ev$vectors
## Symmetric & positive definite
##Factorization: of A
V %*% diag(L) %*% t(V)
diag(L)
zapsmall(t(V) %*% A %*% V)
## Diagonalization : V diagonalizes A
#diag(L)
zapsmall(t(V) %*% A %*% V)
## Choleski factorization
R <- chol(A) # A=R'R (A real & symmetric)
t(R)%*%R
##**************************************************************

You might also like