【问题标题】:How to interpolate data from unevenly spaced array in Julia?如何在 Julia 中从不均匀间隔的数组中插入数据?
【发布时间】:2020-01-31 03:55:09
【问题描述】:

如何在 Julia 中使用最近邻插值构造不均匀间隔数据的插值?我查看了the documentation 并在 REPL 中输入了以下内容:

using Interpolations
input = [1.0 60; 1.1 0; 2.0 60; 2.3 0; 4.0 430; 4.05 0]
itp   = interpolate(input[:,1], input[:,2], Gridded(Constant()))

这对我来说似乎很简单,但给出了:

错误:LoadError:MethodError:没有方法匹配 interpolate(::Array{Float64,1}, ::Array{Float64,1}, ::Gridded{Constant})

我需要将Arrays 转换为Vectors 吗?如果是这样,怎么做?请告诉我有一个简单的解决方案...

【问题讨论】:

    标签: julia interpolation


    【解决方案1】:

    当你在一个 N 维空间中时,你应该把插值“结”的坐标放在一个 N 维向量元组中。在一维中,这意味着像(x,) 这样的一元组,而不是普通的x

    julia> input = [1.0 60; 1.1 0; 2.0 60; 2.3 0; 4.0 430; 4.05 0]
    6×2 Array{Float64,2}:
     1.0    60.0
     1.1     0.0
     2.0    60.0
     2.3     0.0
     4.0   430.0
     4.05    0.0
    
    julia> x = input[:, 1];
    julia> y = input[:, 2];
    julia> itp = interpolate((x,), y, Gridded(Constant()))
    6-element interpolate((::Array{Float64,1},), ::Array{Float64,1}, Gridded(Constant())) with element type Float64:
      60.0
       0.0
      60.0
       0.0
     430.0
       0.0
    
    julia> itp(1.01)
    60.0
    

    【讨论】:

    • 谢谢,这行得通。但是在测试之后,我注意到我正在寻找一个使用 previous 值的分段插值,类似于Matlab'sgriddedInterpolant(___, 'previous')。在 Julia 中也可以这样做吗?
    • 目前我不确定Interpolation 中是否存在这样的功能。但是,它应该很容易实现。也许这会是一个受欢迎的 PR?
    • 公关,你的意思是“拉请求”?好像是one already exists,不过还在等待中……
    • 是的,我就是这个意思。很高兴看到该功能在雷达中......
    猜你喜欢
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 2021-10-19
    • 1970-01-01
    • 2017-03-14
    相关资源
    最近更新 更多