Fast computation of kernel matrix in R

后端 未结 1 981
南旧
南旧 2021-01-12 19:56

I have an n x p matrix and would like to compute the n x n matrix B defined as

B[i, j] = f(A[i,], A[j,])

where f is a function that accept

相关标签:
1条回答
  • 2021-01-12 20:03

    You can use outer with the matrix dimensions.

    n <- 10
    p <- 5
    A <- matrix( rnorm(n*p), n, p )
    f <- function(x,y) sqrt(sum((x-y)^2))
    B <- outer( 
      1:n, 1:n, 
      Vectorize( function(i,j) f(A[i,], A[j,]) ) 
    )
    
    0 讨论(0)
提交回复
热议问题