0

我一直在计算数组每个点的积分。这个想法首先是创建一个函数(“Integrand”)。然后,创建计算必要积分的第二个函数(“MyConvolve”)。

这是我到目前为止所做的:

Integrand = function(s,x)
{ 1/4*(abs(x-s)<=1)*(abs(s)<=1) }

MyConvolve = function(func,data)
{ return( integrate(func, lower=-Inf, upper=Inf, data) ) }

现在,使用一些数组运行代码,我收到一条错误消息:

SomeMatrix = replicate(10, rnorm(10))
MyConvolve(Integrand, SomeMatrix)

最终出现以下错误消息:

Error in integrate(func, lower = -Inf, upper = Inf, data) :
evaluation of function gave a result of wrong length

我已经尝试过对该函数进行矢量化,但最终还是得到了错误消息。

非常感谢您的帮助!

4

1 回答 1

0

I am not sure I understand what you are trying to compute, but if you want to evaluate MyConvolve(Integrand,s), where s takes all the values in SomeMatrix, then apply is sufficient.

sapply( SomeMatrix, function(s) MyConvolve( Integrand, s )$value )

However, the dimensions of the matrix are lost. You can recover them as follows:

result <- SomeMatrix
result[] <- sapply( SomeMatrix, function(s) MyConvolve( Integrand, s )$value )
于 2013-04-10T11:46:08.543 回答