Open In App

Calculate the outer product of two vectors using R

Last Updated : 25 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will explore different ways to Calculate the outer product of two vectors using R Programming Language.

What is the outer product of two vectors

the outer product of two vectors is a matrix where each element is the product of a corresponding element from the first vector with a corresponding element from the second vector. The result of the outer product of two vectors is a matrix where each element is the product of a pair of elements from the two input vectors. If the two vectors have dimensions n and m, then their outer product is an n×m matrix.

for example, Given two vectors a=[a1, a2, a3] and b=[b1, b2, b3] their outer product will be a matrix:

a×b=|a1b1 a1b2 a1b3|
|a2b1 a2b2 a2b3|
|a3b1 a3b2 a3b3|

below are different ways by which we can Calculate the outer product of two vectors:

  1. Using outer function
  2. Using for loop
  3. Using sapply

Calculate the outer product Using outer function

In this approach we will make use The outer function in R which is used to computes the outer product of two arrays or vectors.

in below example we have used outer function to calculate the outer product of two vectors.

R
# Define two vectors
x <- c(1, 2, 3)
y <- c(4, 5, 6)

# Calculate the outer product
outer_product <- outer(x, y)
print("outer product is: ")
print(outer_product)

Output:

[1] "outer product is: "
[,1] [,2] [,3] [1,] 4 5 6 [2,] 8 10 12 [3,] 12 15 18

Calculate the outer product Using for loop

In this approach, we first create an empty matrix with dimensions corresponding to the lengths of vectors. Then, we use nested loops to iterate over each element of the vectors and calculate the product.

below example show how we can calulate outer product using for loop.

R
# Define two vectors
x <- c(2, 3, 5)
y <- c(4, 1, 6)

# Initialize an empty matrix to store the outer product
outer_product <- matrix(0, nrow = length(x), ncol = length(y))

# Calculate the outer product using loops
for (i in 1:length(x)) {
  for (j in 1:length(y)) {
    outer_product[i, j] <- x[i] * y[j]
  }
}
print("outer product is: ")
print(outer_product)

Output:

[1] "outer product is: "
[,1] [,2] [,3] [1,] 8 2 12 [2,] 12 3 18 [3,] 20 5 30

Calculate the outer product Using sapply

In this approach we will use sapply to iterate over each element of one vector and multiply it with the other vector to calculate the outer product.

in below example we will be using sapply to iterate and multiply. At end we will transpose the matrix to convert it into outer product.

R
# Define two vectors
x <- c(1, 2, 3)
y <- c(4, 5, 6)

# Calculate the outer product using sapply
outer_product <- sapply(x, function(i) y * i)

# Transpose the result to convert rows to columns
outer_product <- t(outer_product)

print(outer_product)

Output:

     [,1] [,2] [,3]
[1,]    4    5    6
[2,]    8   10   12
[3,]   12   15   18

Conclusion

In conclusion, we have seen there are several approaches to calculate the outer product of two vectors in R. Each approach has its own advantages and can be chosen based on the specific requirements of your problem.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads