Vectors and Matrices in R

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

VECTORS AND MATRICES IN R

AIM:

To create vectors and matrices in R.

SOURCE CODE:

P <- matrix(c(5:16), nrow = 4, byrow = TRUE)


print(P)
Q <- matrix(c(3:14), nrow = 4, byrow = FALSE)
print(Q)
#assigning row names and column names
rownames=c("row1","row2","row3","row4")
colnames=c("col1","col2","col3")
dimnames(P)=list(rownames,colnames)
print(P)
#accessing element from matrix
P[1,3]
Q[1,]
P[,3]
#modification of matrix-assigning a single element
P[2,2]=20
print(P)
#modification of matrix-modifying multiple elements
Q[Q>=11]=0
print(Q)
#modification of matrix-addition of rows
P=rbind(P,c(17,18,19))
print(P)
#modification of matrix-addition of columns
Q=cbind(Q,c(20,21,22,23))
Q
#matrix operations
R=matrix(c(1:16),4,4)
R
#addition
a=R+Q
a
#Subtraction
b=R-Q
b
#Multiplication
c=R*Q
c
#Division
d=R/Q
d
#Multiplication by constant
e=R*5
e

OUTPUT:
RESULT:
Thus the program was completed and the output was verified successfully.

You might also like