R中内核矩阵的快速计算 [英] Fast computation of kernel matrix in R

查看:87
本文介绍了R中内核矩阵的快速计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个n x p矩阵,想计算定义为的n x n矩阵B

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,])

其中f是一个函数,它接受适当维数的参数.有一个巧妙的技巧可以在R中进行计算吗? f是对称且正定的(如果这可以帮助计算).

where f is a function that accepts arguments of the appropriate dimensionality. Is there a neat trick to compute this in R? f is symmetric and positive-definite (if this can help in the computation).

Praneet要求指定f.这是一个好点.尽管我认为对任何函数都有一个有效的解决方案会很有趣,但是在f(x,y)是base :: norm(xy,type ='F' ).

Praneet asked to specify f. That is a good point. Although I think it would be interesting to have an efficient solution for any function, I would get a lot of mileage from efficient computation in the important case where f(x, y) is base::norm(x-y, type='F').

推荐答案

您可以将outer用于矩阵尺寸.

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,]) ) 
)

这篇关于R中内核矩阵的快速计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆