0% found this document useful (0 votes)
14 views12 pages

Matrix Operations in R

The document discusses various matrix operations in R, including addition, subtraction, transposition, multiplication, and solving matrices. It explains how to perform element-wise and matrix multiplication, take the transpose, power, and inverse of matrices using functions like t(), %*%, crossprod(), tcrossprod(), %^%, and solve(). It also introduces the cbind() and rbind() functions for combining matrices by columns or rows.

Uploaded by

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

Matrix Operations in R

The document discusses various matrix operations in R, including addition, subtraction, transposition, multiplication, and solving matrices. It explains how to perform element-wise and matrix multiplication, take the transpose, power, and inverse of matrices using functions like t(), %*%, crossprod(), tcrossprod(), %^%, and solve(). It also introduces the cbind() and rbind() functions for combining matrices by columns or rows.

Uploaded by

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

MATRIX

OPERATIONS
ADDITION AND SUBTRACTION

• The most basic matrix operations are addition and subtraction. In the following examples
we are going to use the square matrices of the following block of code
• A <- matrix(c(10, 8, 5, 12), ncol = 2, byrow = TRUE)
• A

• B <- matrix(c(5, 3,15, 6), ncol = 2, byrow = TRUE)


• B
• #A #B
• [, 1] [, 2] [, 1] [, 2]
• [1, ] 10 8 [1, ] 5 3
• [2, ] 7 12 [2, ] 15 6
• dim(A) # 2 2
• dim(B) # 2 2
• On the one hand, with the + operator you can compute an element-wise sum of the two
matrices:
• A+B
• [, 1] [, 2]
• [1, ] 15 11
• [2, ] 20 18
• On the other hand, the - operator will allow you to substract them:
• A–B
• [, 1] [, 2]
• [1, ] 5 5
• [2, ] -10 6
TRANSPOSE A MATRIX IN R

• To find the transpose of a matrix in R you just need to use the t function as follows:
• t(A)
• [, 1] [, 2]
• [1, ] 10 5
• [2, ] 8 12
MATRIX MULTIPLICATION IN R
• Multiplication by a scalar
• In order to multiply or divide a matrix by a scalar you can make use of the * or /
operators, respectively:
• 2*A
• [, 1] [, 2]
• [1, ] 20 16
• [2, ] 10 24
• A/2
• [, 1] [, 2]
• [1, ] 5.0 4
• [2, ] 2.5 6
ELEMENT-WISE MULTIPLICATION
• The element-wise multiplication of two matrices of the same dimensions can also be
computed with the * operator. The output will be a matrix of the same dimensions of the
original matrices.
• A*B
• [, 1] [, 2]
• [1, ] 50 24
• [2, ] 75 72
MATRIX MULTIPLICATION

• In R, a matricial multiplication can be performed with the %*% operator.


• A %*% B
• [, 1] [, 2]
• [1, ] 170 78
• [2, ] 205 87
MATRIX CROSSPRODUCT

• If you need to calculate the matricial product of a • tcrossprod(A, B)


matrix and the transpose or other you can type t(A) %*
% B or A %*% t(B), being A and B the names of the • Equivalent to A %*% t(B)
matrices. However, in R it is more efficient and faster
using the crossprod and tcrossprod functions, • [,1 ] [, 2]
respectively.
• [1, ] 74 198
• crossprod(A, B)
• Equivalent to t(A) %*% B • [2, ] 61 147
• [, 1] [, 2]
• [1, ] 125 60
• [2, ] 220 96
POWER OF A MATRIX IN R

• Power of A
• There is no a built-in function in base R to
calculate the power of a matrix, so we will • [, 1] [, 2]

provide two different alternatives. • [1, ] 140 176

• [2, ] 110 184

• On the one hand, you can make use of the %^ • On the other hand the matrixcalc package provides the
matrix.power function:
% operator of the expm package as follows:
• # install.packages("matrixcalc")
• # install.packages("expm")
• library(matrixcalc)
• library(expm)
• matrix.power(A, 2)
• A %^% 2
• You can check that the power is correct with the following
code: A %*% A
SOLVE FUNCTION

• R programming has a dedicated function to do the task for you. The solve() function in R
programming takes a matrix as an argument and then returns the inverse of that matrix.
THE CBIND() AND RBIND() FUNCTION IN R


• The cbind() function in R is a function that allows you to create or modify the matrices. The
function either takes vectors or matrices as an input and then combines those as columns to create
a new matrix. If the matrices are provided as an argument, it concatenates the two matrices
column-wise.

• On the similar lines the rbind() function in R allows you to create or modify the matrices. It either
takes vectors or matrices as an input and then combines those as rows to create a new matrix. If the
matrices are provided as an input, this function concatenates them row-wise.

You might also like