Matrices in R programming are used for
storing homogeneous data that can be accessed and manipulated easily. Matrix
operations are used for performing various arithmetic and logical operations on
matrices in R.
Ø The common matrix
operations:
l Addition
and Subtraction: Matrices can be added and subtracted
using the + and - operators. The matrices should have the same dimensions for
addition and subtraction.
Syntax: mat1
+ mat2 & mat1 - mat2
l Multiplication:
Matrices can be multiplied using the %*% operator. The number of columns in the
first matrix should be equal to the number of rows in the second matrix.
Syntax: mat1
%*% mat2
l Transpose:
The
transpose of a matrix can be obtained using the t() function.
Syntax: t(mat)
l Inverse:
The inverse of a matrix can be obtained using the solve() function. The matrix
should be a square matrix and should be invertible.
Syntax: solve(mat)
Determinant:
The determinant of a matrix can be obtained using the det() function. The
matrix should be a square mat
Syntax: det(mat)
Eigenvalues
and Eigenvectors: The eigenvalues and eigenvectors of a
matrix can be obtained using the eigen() function.
Syntax: eigen(mat)
Diagonal:
The
diagonal elements of a matrix can be obtained using the diag() function.
Syntax: diag(mat)
Row
and Column Operations: Rows and columns of a matrix can be
selected and operated upon using the [] operator.
Syntax:
mat[row,] # Row Operations
mat[,col] # Column Operations
Example:
mat1
<- matrix(1:6, nrow=2) #
Create two matrices
mat2
<- matrix(7:12, nrow=2)
mat3
<- mat1 + mat2 #
Addition of two matrices
cat("Addition
of two matrices:\n", mat3, "\n")
mat4
<- mat1 - mat2 #
Addition of two matrices
cat("Subtraction
of two matrices:\n", mat4, "\n")
mat5
<- mat1 %*% mat2 #
Multiplication of two matrices
cat("Multiplication
of two matrices:\n", mat5, "\n")
mat6
<- t(mat1) #
Transpose of a matrix
cat("Transpose
of a matrix:\n", mat6, "\n")
mat7
<- solve(mat1) #
Inverse of a matrix
cat("Inverse
of a matrix:\n", mat7, "\n")
mat8
<- det(mat1) #
Determinant of a matrix
cat("Determinant
of a matrix:\n", mat8, "\n")
mat9
<- eigen(mat1) #
Eigenvalues and eigenvectors of a matrix
cat("Eigenvalues
of a matrix:\n", mat9$values, "\n")
cat("Eigenvectors
of a matrix:\n", mat9$vectors, "\n")
mat10
<- diag(mat1) #
Diagonal of a matrix
cat("Diagonal
of a matrix:\n", mat10, "\n")
mat11
<- mat1[1,] #
Select a row of a matrix
cat("Selected
row of a matrix:\n", mat11, "\n")
mat12
<- mat1[,2] #
Select a column of a matrix
cat("Selected
column of a matrix:\n", mat12, "\n")
Output
:
Addition
of two matrices:
[,1]
[,2] [,3]
[1,]
8 10 12
[2,]
14 16 18
Subtraction
of two matrices:
[,1]
[,2] [,3]
[1,]
-6 -6 -6
[2,]
-6 -6 -6
Multiplication
of two matrices:
[,1]
[,2] [,3]
[1,]
58 64 70
[2,]
139 154 169
Transpose
of a matrix:
[,1]
[,2] [,3]
[1,]
1 3 5
[2,]
2 4 6
Inverse
of a matrix:
[,1]
[,2]
[1,]
-1.66667 1.16667
[2,]
1.33333 -0.66667
Determinant
of a matrix:
[1]
-2
Eigenvalues
of a matrix:
[1]
-0.3722813 8.3722813
Eigenvectors
of a matrix:
[,1]
[,2]
[1,]
-0.824564 0.4159736
[2,]
0.565767 0.9093767
Diagonal
of a matrix:
[1]
1 4
Selected
row of a matrix:
[1]
1 3 5
Selected
column of a matrix:
[1] 2
4
No comments:
Post a Comment