Bing
Bing

Reputation: 23

How can I multiply columns by columns from different matrix in R?

guys: I have two matrix as following:

d <- cbind(c(1,2,3,4),c(1,1,1,1),c(1,2,4,8))
v <- cbind(c(2,2,2,2),c(3,3,3,3))

But I want to get a matrix consisted of divj as following:

    d1v1 d1v2 d2v1 d2v2 d3v1 d3v2
    2     3    2    3    2    3
    4     6    2    3    4    6
    6     9    2    3    8    12
    8    12    2    3    16   24

This is an example of my question,I wonder if you can tell me how to write codes to solve this question.Many thanks.

Upvotes: 2

Views: 45

Answers (1)

Carpa
Carpa

Reputation: 458

matrix(apply(v,2,function(x){x*d}),4,6)

Upvotes: 1

Related Questions