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
You can use outer with the matrix dimensions.
outer
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,]) ) )