How to do R multiplication with Nx1 1xM for Matrix NxM?

后端 未结 3 1615
野性不改
野性不改 2020-12-20 09:28

I want to do a simple column (Nx1) times row (1xM) multiplication, resulting in (NxM) matrix. Code where I create a row by sequence, and column by transposing a similar seq

3条回答
  •  礼貌的吻别
    2020-12-20 09:39

    Here's a comparison of the execution speed for the three methods when the vectors being used are of length 100. The fastest is tcrossprod, with x%*%t(y) taking 17% longer and outer(x,y) taking 45% longer (in median time). In the table, neval is the number of times the function was evaluated to get the benchmark scores.

    > x <- runif(100,0,100)
    > y <- runif(100,0,100)
    > microbenchmark(tcrossprod(x,y), x%*%t(y), outer(x,y), times=5000)
    Unit: microseconds
                 expr    min      lq     mean  median      uq       max neval
     tcrossprod(x, y) 11.404 16.6140 50.42392 17.7300 18.7555  5590.103  5000
           x %*% t(y) 13.878 19.4315 48.80170 20.5405 21.7310  4459.517  5000
          outer(x, y) 19.238 24.0810 72.05250 25.3595 26.8920 89861.855  5000
    

    To get the the following graph, have

    library("ggplot2")
    bench <- microbenchmark(tcrossprod(x,y), x%*%t(y), outer(x,y), times=5000)
    autplot(bench)
    

    Edit: The performance depends on the size of x and y, and of course the machine running the code. I originally did the benchmark with vectors of length 100 because that's what Masi asked about. However, it appears the three methods have very similar performance for larger vectors. For vectors of length 1000, the median times of the three methods are within 5% of each other on my machine.

    > x <- runif(1000)
    > y <- runif(1000)
    > microbenchmark(tcrossprod(x,y),x%*%t(y),outer(x,y),times=2000)
    Unit: milliseconds
                 expr      min       lq     mean   median       uq       max neval
     tcrossprod(x, y) 1.870282 2.030541 4.721175 2.916133 4.482346  75.77459  2000
           x %*% t(y) 1.861947 2.067908 4.921061 3.067670 4.527197 105.60500  2000
          outer(x, y) 1.886348 2.078958 5.114886 3.033927 4.556067  93.93450  2000
    

提交回复
热议问题