How to multiply two vectors in R as in mathematics?

R ProgrammingServer Side ProgrammingProgramming

In mathematics, when two vectors are multiplied the output is a scalar quantity which is the sum of the product of the values. For example, if we have two vectors x and y each containing 1 and 2 then the multiplication of the two vectors will be 5. In R, we can do it by using t(x)%*%y.

Example1

 Live Demo

x1<-1:2
y1<-1:2
t(x1)%*%y1

Output

[,1]
[1,] 5

Example2

 Live Demo

x2<-1:100
y2<-1:100
t(x2)%*%y2

Output

[,1]
[1,] 338350

Example3

 Live Demo

x3<-sample(0:5,120,replace=TRUE)
x3
[1] 2 4 1 4 5 0 2 2 0 4 4 5 5 1 4 2 4 2 0 0 0 2 0 1 5 2 5 4 3 5 1 2 1 1 3 3 2
[38] 1 0 3 5 3 0 0 5 5 5 3 3 5 4 3 4 5 4 3 5 1 0 5 4 0 5 1 3 5 0 3 2 2 3 3 0 0
[75] 0 4 4 1 5 3 4 3 5 2 2 4 0 3 2 4 0 4 5 3 1 2 0 0 0 2 2 2 3 3 1 2 4 2 3 2 0
[112] 5 2 1 5 3 4 1 5 1
y3<-sample(0:5,120,replace=TRUE)
y3
[1] 2 0 3 0 2 4 5 3 0 4 5 2 4 3 3 3 4 2 3 2 1 5 0 5 2 4 4 1 3 2 2 1 5 3 0 2 0
[38] 2 2 1 1 3 1 3 3 2 3 3 0 1 4 5 1 2 3 3 5 2 5 2 1 0 0 5 0 3 1 5 2 4 3 4 2 2
[75] 4 4 1 4 2 4 2 2 4 5 0 3 5 3 4 1 1 4 0 1 1 4 1 3 5 3 5 5 5 2 5 1 5 0 0 2 0
[112] 0 3 2 0 4 2 4 4 3
t(x3)%*%y3

Output

[,1]
[1,] 766

Example4

 Live Demo

x4<-sample(1:10,120,replace=TRUE)
x4
[1] 4 2 3 4 4 7 4 10 7 5 8 5 6 7 6 10 10 9 6 10 3 3 10 10 4
[26] 10 2 5 7 2 2 4 2 10 2 9 9 1 5 2 8 2 8 1 5 10 4 6 3 9
[51] 10 7 6 2 7 10 9 7 9 3 1 10 9 1 9 3 6 10 1 8 5 2 6 3 8
[76] 6 3 6 5 9 8 2 5 7 8 6 4 4 6 10 3 5 2 10 7 6 8 4 7 8
[101] 4 9 3 5 5 2 7 5 2 5 6 3 10 9 3 1 2 10 10 4
y4<-sample(1:10,120,replace=TRUE)
y4
[1] 10 10 7 6 9 7 1 7 7 1 2 9 6 8 2 7 8 5 8 6 1 3 1 5 8
[26] 1 9 2 7 4 4 9 6 3 5 1 1 4 5 9 1 8 6 9 7 4 6 4 9 1
[51] 4 10 7 10 8 7 5 2 1 3 8 10 10 3 1 6 8 5 9 1 9 5 1 3 9
[76] 2 2 3 7 8 5 5 3 5 4 7 7 2 4 9 3 8 3 7 6 3 7 2 7 4
[101] 1 6 3 4 6 1 1 10 3 4 8 2 5 1 3 3 7 7 5 7
t(x4)%*%y4

Output

[,1]
[1,] 3532

Example5

 Live Demo

x5<-sample(101:110,120,replace=TRUE)
x5
[1] 103 109 110 109 107 107 105 107 101 105 101 105 106 102 108 109 103 107
[19] 105 105 108 105 104 103 105 103 106 108 103 110 104 109 105 103 107 109
[37] 105 102 103 106 105 110 105 110 104 110 107 105 105 101 106 109 103 106
[55] 103 107 104 102 101 108 103 101 104 110 104 104 103 107 102 110 101 107
[73] 110 103 103 101 107 108 107 106 105 101 103 102 106 110 108 103 101 102
[91] 107 106 108 109 108 108 109 107 109 105 107 105 106 110 103 104 103 103
[109] 103 101 107 102 107 106 107 109 102 101 102 102
y5<-sample(101:110,120,replace=TRUE)
y5
[1] 107 106 107 108 104 110 107 110 104 105 107 107 101 103 109 110 110 108
[19] 107 110 103 110 103 106 108 103 106 103 101 108 104 103 103 106 103 110
[37] 102 102 104 103 101 109 104 106 109 108 105 110 110 101 103 110 102 101
[55] 102 102 108 110 105 104 109 105 109 101 109 106 107 104 105 107 104 110
[73] 107 106 108 103 104 102 106 101 103 106 104 101 107 101 104 109 107 110
[91] 110 107 109 107 105 103 107 103 104 107 102 103 103 102 109 104 104 101
[109] 104 106 109 108 106 106 104 104 101 101 110 103
t(x5)%*%y5

Output

[,1]
[1,] 1333499

Example6

 Live Demo

x6<-sample(51:60,120,replace=TRUE)
x6
[1]  51 53 55 60 58 53 53 58 51 59 59 51 58 58 52 55 51 60 56 59 56 56 58 56 56
[26] 58 52 54 60 54 55 51 56 55 53 59 58 59 60 58 56 58 56 54 56 51 60 58 58 53
[51] 53 52 53 51 55 53 57 59 55 53 53 56 54 52 57 58 51 55 58 52 54 53 52 54 59
[76] 54 56 58 52 51 59 59 56 51 56 53 59 51 53 53 59 57 53 53 55 52 52 51 52 58
[101] 56 52 57 60 52 53 59 54 55 53 60 55 52 58 51 58 52 52 53 57
y6<-sample(0:1,120,replace=TRUE)
y6
[1] 1 0 0 0 0 0 1 0 0 0 0 1 1 0 1 1 1 0 1 0 1 0 1 1 1 0 1 1 1 0 1 1 1 0 0 0 1
[38] 1 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 1 1 0 0 1 0 0 1 0 0 0 0 0 0 1 1 0 0 0
[75] 1 0 0 0 0 0 1 1 0 0 1 0 0 0 0 0 0 0 1 0 0 1 1 0 0 1 1 0 0 0 1 1 1 1 1 1 1
[112] 0 0 0 1 1 0 1 0 0
t(x6)%*%y6

Output

[,1]
[1,] 2692

Example7

 Live Demo

x7<-sample(c(5,15,25,35,45,55,65,75,85,95),120,replace=TRUE)
x7
[1]  15 75 45 15 35 25 45 45 85 25 75 45 15 5 45 15 45 35 55 45 15 85 45 95 85
[26] 35 75 45 25 55 45 25 95 95 65 75 5 85 25 55 95 65 65 65 45 45 25 15 5 45
[51] 25 25 5 95 55 55 95 95 35 45 25 5 15 95 75 25 65 65 25 35 75 25 75 25 65
[76] 95 75 55 25 5 15 65 65 5 35 55 75 15 95 35 15 95 95 5 55 35 95 5 45 25
[101] 75 75 35 35 45 55 25 65 35 45 35 35 95 85 35 35 5 55 5 85
y7<-sample(c(1,11,21,31,41,51,61,71,81,91),120,replace=TRUE)
y7
[1] 41 31 31 41 71 11 51 31 11 41 41 31 91 71 11 21 91 11 11 11 41 1 31 31 61
[26] 21 51 41 31 11 81 31 71 91 21 1 21 11 31 11 41 71 71 21 51 41 31 81 81 41
[51] 71 31 11 51 71 61 41 81 61 61 21 71 41 91 1 61 91 21 21 61 41 11 91 81 31
[76] 1 31 31 71 41 41 61 71 11 51 71 41 81 91 81 31 31 41 61 21 91 91 61 1 31
[101] 81 51 91 51 81 71 41 31 21 41 71 61 41 71 91 21 71 21 51 1
t(x7)%*%y7

Output

[,1]
[1,] 264460

Example8

 Live Demo

x8<-rnorm(25,5,0.04)
x8
[1] 5.044193 4.965799 5.016561 4.936845 5.091920 4.978903 4.975891 4.981731
[9] 5.002986 5.003291 5.049467 4.940315 5.022184 5.029431 5.025540 5.066160
[17] 5.002058 4.969025 4.995391 5.006298 5.044991 4.993655 5.017182 5.020555
[25] 4.939784
y8<-rnorm(25,5,0.08)
y8
[1] 5.103655 4.963152 5.184913 5.150147 4.977731 5.040406 5.104955 5.062985
[9] 5.036312 5.082053 5.019926 5.009339 5.031032 4.851256 4.876433 4.908972
[17] 4.957003 5.039052 5.048009 5.030555 5.095683 4.898264 4.943438 4.901391
[25] 5.007738
t(x8)%*%y8

Output

[,1]
[1,] 627.2037
raja
Published on 17-Oct-2020 13:28:17
Advertisements