How do I use %*% to multiply two vectors in order to produce a matrix?

I have two sets of numbers, and I want to multiply them to produce a matrix.

Suppose each set of numbers contains 5 values, so I know my matrix should be 5^2=25 values.

#First allocate an empty matrix?
mymat <- matrix(nrow=5, ncol=5)

#Let's say these are the two sets of numbers
set1 <- seq(1,5)
set2 <- seq(6,10)

#Set the row and col names of the matrix appropriately
rownames(mymat) <- set1
colnames(mymat) <- set2


Next I just need to multiply columns and rows to fill in the values. I am reading online about %*% but I can't seem to make it work across an entire matrix.

I'm trying to produce this:
image

set1 <- seq(1,5)
set2 <- seq(6,10)
set1 %*% t(set2)
     [,1] [,2] [,3] [,4] [,5]
[1,]    6    7    8    9   10
[2,]   12   14   16   18   20
[3,]   18   21   24   27   30
[4,]   24   28   32   36   40
[5,]   30   35   40   45   50
2 Likes

I didn't even think about transposing! This works beautifully thank you!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.