【问题标题】:Interpolating data in R在 R 中插值数据
【发布时间】:2013-04-08 04:02:09
【问题描述】:

假设我在 R 中有一个 3 x 5 的矩阵:

4  5  5  6  8
3  4  4  5  6
2  3  3  3  4

我想在这些值之间进行插值以创建大小为 15 x 25 的矩阵。我还想指定插值是线性的、高斯的等。我该怎么做?

例如,如果我有一个像这样的小矩阵

2 3
1 3

我希望它变成 3 x 3,那么它可能看起来像

  2    2.5   3
  1.5  2.2   3
  1    2     3 

【问题讨论】:

  • 不清楚你想要什么。请提供示例输出。
  • 这篇文章可能有帮助吗? stackoverflow.com/questions/3093455/…
  • @Arun 这似乎很有用,尽管 R 中必须有一个内置函数才能做到这一点......

标签: r gaussian interpolation


【解决方案1】:
app <- function(x, n) approx(x, n=n)$y # Or whatever interpolation that you want

apply(t(apply(x, 1, function(x) app(x, nc))), 2, function(x) app(x, nr))
     [,1] [,2] [,3]
[1,]  2.0 2.50    3
[2,]  1.5 2.25    3
[3,]  1.0 2.00    3

【讨论】:

  • 在我的回答中跟进我的 cmets:如果我正确阅读此代码,将 ncnr 设置为您想要的输出矩阵的列数/行数将允许任意不对称扩张。
【解决方案2】:

很久以前我写了一个类似的玩具,只是我从来没有开始定义插值函数。还有raster::disaggregate

zexpand<-function(inarray, fact=2, interp=FALSE,  ...)  {
# do same analysis of fact to allow one or two values, fact >=1 required, etc.
fact<-as.integer(round(fact))
switch(as.character(length(fact)),
            '1' = xfact<-yfact<-fact,
            '2'= {xfact<-fact[1]; yfact<-fact[2]},
            {xfact<-fact[1]; yfact<-fact[2];warning(' fact is too long. First two values used.')})
if (xfact < 1) { stop('fact[1] must be > 0') } 
if (yfact < 1) { stop('fact[2] must be > 0') }
bigtmp <- matrix(rep(t(inarray), each=xfact), nrow(inarray), ncol(inarray)*xfact, byr=T)  #does column expansion
bigx <- t(matrix(rep((bigtmp),each=yfact),ncol(bigtmp),nrow(bigtmp)*yfact,byr=T))
# the interpolation would go here. Or use interp.loess on output (won't
# handle complex data). Also, look at fields::Tps which probably does
# a much better job anyway.  Just do separately on Re and Im data
return(invisible(bigx))
}

【讨论】:

  • 感谢您的回复。这与上述 Math Lundberg 的回答有何不同或更好?
  • @CodeGuy 如果我真的在其中放置了一个插值函数(基本上替换了仅复制行或列的 rep 函数),它将做几乎完全相同的事情,除了我允许列维度和行维度的不同扩展。正如 Matthew 所写,您也可以在代码中交换任何您想要的插值器。
  • 我明白了。感谢您的回复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-20
  • 2021-03-06
  • 2021-08-05
  • 1970-01-01
  • 2014-10-20
  • 1970-01-01
相关资源
最近更新 更多