# How to multiply each element of a larger vector with a smaller vector in R?

To multiply each element of a larger vector with a smaller vector, we can perform outer product calculation with the help of %o% operator.

For example, if we have two vectors say x and y where x is of shorter length than y then we can multiply each element of y with each element of x by using the command given below −

x%o%y

Check out the below examples to understand how it works.

## Example 1

To multiply each element of a larger vector with a smaller vector, use the code given below −

x1<-rpois(10,2)
x1

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

[1] 0 5 1 2 3 1 1 2 4 1


To multiply each element of a larger vector with a smaller vector, add the following code to the above −

y1<-rpois(12,2)
y1

If you execute all the above given codes as a single program, it generates the following output −

[1] 1 0 1 1 1 2 2 2 2 4 3 1


To multiply each element of a larger vector with a smaller vector, add the following code to the above −

x1%o%y1

## Output

If you execute all the above given codes as a single program, it generates the following output −

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

## Example 2

To multiply each element of a larger vector with a smaller vector, use the code given below −

x2<-rpois(8,5)
y2<-rpois(10,2)
x2%o%y2

## Output

If you execute all the above given codes as a single program, it generates the following output −

     [,1][,2][,3][,4][,5][,6][,7][,8][,9][,10]
[1,]  2   4   6   0   8   2   12  4   0   2
[2,]  4   8  12   0  16   4   24  8   0   4
[3,]  7  14  21   0  28   7   42 14   0   7
[4,]  2   4   6   0   8   2   12  4   0   2
[5,]  6  12  18   0  24   6   36 12   0   6
[6,]  8  16  24   0  32   8   48 16   0   8
[7,]  5  10  15   0  20   5   30 10   0   5
[8,]  6  12  18   0  24   6   36 12   0   6

## Example 3

To multiply each element of a larger vector with a smaller vector, use the code given below −

x3<-rpois(10,5)
y3<-rpois(11,2)
x3%o%y3

## Output

If you execute all the above given codes as a single program, it generates the following output −

     [,1][,2][,3][,4][,5][,6][,7][,8][,9][,10][,11]
[1,]   6   9   6   6  3   9   6   15   6   6   21
[2,]  10  15  10  10  5  15  10   25  10  10   35
[3,]  10  15  10  10  5  15  10   25  10  10   35
[4,]  16  24  16  16  8  24  16   40  16  16   56
[5,]   6   9   6   6  3   9   6   15   6   6   21
[6,]  14  21  14  14  7  21  14   35  14  14   49
[7,]  16  24  16  16  8  24  16   40  16  16   56
[8,]  12  18  12  12  6  18  12   30  12  12   42
[9,]   4   6   4   4  2   6   4   10   4   4   14
[10,] 18  27  18  18  9  27  18   45  18  18   63

## Example 4

To multiply each element of a larger vector with a smaller vector, use the code given below −

x4<-rpois(8,1)
y4<-rpois(10,2)
x4%o%y4

## Output

If you execute all the above given codes as a single program, it generates the following output −

   [,1][,2][,3][,4][,5][,6][,7][,8][,9][,10]
[1,] 0  0   0   0   0   0   0   0   0   0
[2,] 0  0   0   0   0   0   0   0   0   0
[3,] 8  4   2   4   6   0   2   2   2   4
[4,] 0  0   0   0   0   0   0   0   0   0
[5,] 8  4   2   4   6   0   2   2   2   4
[6,] 8  4   2   4   6   0   2   2   2   4
[7,] 0  0   0   0   0   0   0   0   0   0
[8,] 4  2   1   2   3   0   1   1   1   2

## Example 5

To multiply each element of a larger vector with a smaller vector, use the code given below −

x5<-rpois(10,5)
y5<-rpois(12,10)
x5%o%y5

## Output

If you execute all the above given codes as a single program, it generates the following output −

    [,1] [,2][,3][,4][,5][,6][,7][,8][,9][,10][,11][,12]
[1,]  24  48  52  56  32  80  48  36  36  32  40   44
[2,]  18  36  39  42  24  60  36  27  27  24  30   33
[3,]  12  24  26  28  16  40  24  18  18  16  20   22
[4,]   6  12  13  14   8  20  12   9   9   8  10   11
[5,]  18  36  39  42  24  60  36  27  27  24  30   33
[6,]  18  36  39  42  24  60  36  27  27  24  30   33
[7,]  60 120 130 140  80 200 120  90  90  80 100  110
[8,]  18  36  39  42  24  60  36  27  27  24  30   33
[9,]  30  60  65  70  40 100  60  45  45  40  50   55
[10,] 36  72  78  84  48 120  72  54  54  48  60   66

Updated on: 11-Nov-2021

110 Views