How to find the cross product of two vectors in R by adding the elements?

R ProgrammingServer Side ProgrammingProgramming

To find the cross product of two vectors in R by adding the elements, we can calculate outer product by using %o% operator.

For example, if we have two vectors say x and y then cross product of these two vectors by adding the elements can be found by using the command given below −

x%o%y

Check out the below examples to understand how it works.

Example 1

To find the cross product of two vectors in R by adding the elements, use the code given below −

x1<-1:5
y1<-1:5
x1%o%y1

Output

If you execute the above given code, it generates the following output −

    [,1] [,2] [,3][,4][,5]
[1,]  1   2   3    4   5
[2,]  2   4   6    8  10
[3,]  3   6   9   12  15
[4,]  4   8  12   16  20
[5,]  5  10  15   20  25

Example 2

To find the cross product of two vectors in R by adding the elements, use the code given below −

x2<-1:6
y2<-1:6
x2%o%y2

Output

If you execute the above given code, it generates the following output −

   [,1][,2][,3][,4][,5][,6]
[1,] 1   2   3   4   5   6
[2,] 2   4   6   8  10  12
[3,] 3   6   9  12  15  18
[4,] 4   8  12  16  20  24
[5,] 5  10  15  20  25  30
[6,] 6  12  18  24  30  36

Example 3

To find the cross product of two vectors in R by adding the elements, use the code given below −

x3<-1:8
y3<-1:8
x3%o%y3

Output

If you execute the above given code, it generates the following output −

   [,1][,2][,3][,4][,5][,6][,7][,8]
[1,] 1   2   3   4   5   6   7   8
[2,] 2   4   6   8  10  12  14  16
[3,] 3   6   9  12  15  18  21  24
[4,] 4   8  12  16  20  24  28  32
[5,] 5  10  15  20  25  30  35  40
[6,] 6  12  18  24  30  36  42  48
[7,] 7  14  21  28  35  42  49  56
[8,] 8  16  24  32  40  48  56  64

Example 4

To find the cross product of two vectors in R by adding the elements, use the code given below −

x4<-1:10
y4<-1:10
x4%o%y4

Output

If you execute the above given code, it generates the following output −

    [,1][,2][,3][,4][,5][,6][,7][,8][,9][,10]
[1,]  1   2   3   4   5   6   7   8   9  10
[2,]  2   4   6   8  10  12  14  16  18  20
[3,]  3   6   9  12  15  18  21  24  27  30
[4,]  4   8  12  16  20  24  28  32  36  40
[5,]  5  10  15  20  25  30  35  40  45  50
[6,]  6  12  18  24  30  36  42  48  54  60
[7,]  7  14  21  28  35  42  49  56  63  70
[8,]  8  16  24  32  40  48  56  64  72  80
[9,]  9  18  27  36  45  54  63  72  81  90
[10,] 10 20  30  40  50  60  70  80  90 100
raja
Published on 09-Nov-2021 06:38:00
Advertisements