Saturday 7 October 2023

Big Data-Slip11-Slip11 Write a script in R to create two vectors of different lengths and give these vectors as input to array and print addition and subtraction of those matrices.

 Slip11

Write a script in R to create two vectors of different lengths and give these vectors as input to array and print addition and subtraction of those matrices.

 

> v1<-c(10,20,30,40)

> v2<-c(2,3,4)

> matrix1=array(c(v1,v2),dim=(c(2,2,2)))

> print(matrix1)

> matrix2=array(c(v2,v1),dim=(c(2,2,2)))

> print(matrix2)

> matrix1+matrix2

 

> matrix1-matrix2

 

OutPUT

 

 

> v1<-c(10,20,30,40)

> v2<-c(2,3,4)

> matrix1=array(c(v1,v2),dim=(c(2,2,2)))

> print(matrix1)

, , 1

 

     [,1] [,2]

[1,]   10   30

[2,]   20   40

 

, , 2

 

     [,1] [,2]

[1,]    2    4

[2,]    3   10

 

> matrix2=array(c(v2,v1),dim=(c(2,2,2)))

> print(matrix2)

, , 1

 

     [,1] [,2]

[1,]    2    4

[2,]    3   10

 

, , 2

 

     [,1] [,2]

[1,]   20   40

[2,]   30    2

 

> matrix1+matrix2

, , 1

 

     [,1] [,2]

[1,]   12   34

[2,]   23   50

 

, , 2

 

     [,1] [,2]

[1,]   22   44

[2,]   33   12

 

> matrix1-matrix2

, , 1

 

     [,1] [,2]

[1,]    8   26

[2,]   17   30

 

, , 2

 

     [,1] [,2]

[1,]  -18  -36

[2,]  -27    8