outer() equivalent for non-vector lists in R

后端 未结 3 1134
孤独总比滥情好
孤独总比滥情好 2020-12-31 15:38

I understand how outer() works in R:

> outer(c(1,2,4),c(8,16,32), \"*\")

     [,1] [,2] [,3]
[1,]    8   16   32
[2,]   16   32   64
[3,]   32   64  128
         


        
3条回答
  •  爱一瞬间的悲伤
    2020-12-31 15:40

    Just use the for loop. Any built-in functions will degenerate to that anyway, and you'll lose clarity of expression, unless you carefully build a function that generalises outer to work with lists.

    The biggest improvement you could make would be to preallocate the matrix:

    M <- list()
    length(M) <- numElements ^ 2
    dim(M) <- c(numElements, numElements)
    

    PS. A list is a vector.

提交回复
热议问题