0% found this document useful (0 votes)
6 views5 pages

Matrix Operations

The document provides R code for various matrix operations including addition, subtraction, multiplication, division, and matrix inversion. It demonstrates how to create matrices, modify elements, sort, combine, and remove rows or columns from matrices. Additionally, it shows how to transpose a matrix and perform matrix multiplication.

Uploaded by

Anmol Thapa
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)
6 views5 pages

Matrix Operations

The document provides R code for various matrix operations including addition, subtraction, multiplication, division, and matrix inversion. It demonstrates how to create matrices, modify elements, sort, combine, and remove rows or columns from matrices. Additionally, it shows how to transpose a matrix and perform matrix multiplication.

Uploaded by

Anmol Thapa
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/ 5

A=matrix(c(11,22,33,44),

nrow = 2,
ncol = 2,
byrow=FALSE,
dimnames =list(c("a","b"),c("x","y")) )
A
B=matrix(c(5:8),2,2,byrow=FALSE,
dimnames =list(c("c","d"),c("s","t")))
B
print(A+B)
print(A-B)
print(B-A)
print(A*B)
print(A/B)
print(A%*%B) #---------Matrix A X Matrix B

#------------------------
A1=matrix(1:9,3,3)
A1
B1=matrix(0:2,3,1)
B1
#---------A inverse B---------------
a=matrix(c(1,3,2,1,3,1,1,4,3),nrow = 3,byrow=F)
print(a)
b=matrix(c(6,20,13),nrow = 3)
b

a_inv=solve(a)
a_inv

x=a_inv%*%b
x
#-----------Print 1 element----------------------
a[3,1]

#-----------Modify 1 element----------------------
a[3,1]=0
a

#-----------Print 3rd row----------------------


a[3,]

#-----------Modify 3rd row----------------------


a[3,]=c(1,1,1)
a

#------------sort the elements


b=sort(a,decreasing=F)
b
#------------combine 2 matrices-----------------
x=matrix((0:3),2,2)
x

y=matrix(c(3,5,6,0),2,2)
y

z=cbind(x,y) #--------column-wise
z

z1=rbind(x,y) #--------row-wise
z1
#---------To remove row of matrix-------------------
z1=z1[-2,]
z1

#---------To remove column of matrix-------------------


z=z[,-2]
z

#-----------Transpose----------------
tz=t(z)
tz

You might also like