简体   繁体   English

Julia中的Matlab样条函数

[英]Matlab spline function in Julia

I am trying to interpolate two points in Julia, using the same approach of Matlab ( https://uk.mathworks.com/help/matlab/ref/spline.html ). 我试图在Julia中插入两个点,使用相同的Matlab方法( https://uk.mathworks.com/help/matlab/ref/spline.html )。 I have tried the Interpolations ( https://github.com/tlycken/Interpolations.jl ) library, but I am having several issues in creating a working script. 我已经尝试了Interpolations( https://github.com/tlycken/Interpolations.jl )库,但我在创建工作脚本时遇到了一些问题。

I have a DataArrays.DataArray{Float64,1} with two points (let's say 1.5 and 10.5) and 5 NA between them: 我有一个DataArrays.DataArray {Float64,1}有两个点(比方说是1.5和10.5)和它们之间有5个NA:

using DataFrames
using Interpolations

a = @data([1.5, NA, NA, NA, NA, NA, 10.5]);

In Matlab it would be sufficient to run the spline function. 在Matlab中,运行样条函数就足够了。 In Julia, the interpolate function allows cubic interpolations. 在Julia中, 插值函数允许三次插值。 However, it seems not to be working with NAs. 但是,似乎没有与NA合作。 How can I do it? 我该怎么做? Also, do they use the same / an analogous algorithm for interpolate those points? 此外,他们是否使用相同/类似的算法来插入这些点?

If you make the assumption that they are evenly spaced, then you are assuming a linear interpolation for which you can use linspace. 如果你假设它们是均匀间隔的,那么你假设一个线性插值,你可以使用linspace。 You just need the start, the end, and the number of values inbetween: 您只需要开头,结尾和中间值的数量:

linspace(a[1],a[end],sum(isna(a)))

More generally, to do an interpolation between any NAs, you can find the non-NA values with 更一般地说,要在任何NA之间进行插值,您可以找到非NA值

idxs = find(~isna(a))

and then do 然后呢

for i in 1:length(idxs)-1
  tmpidxs = idxs[i]:idxs[i+1]
  a[idxs[i]+1:idxs[i+1]-1] = linspace(a[idxs[i]],a[idxs[i+1]],length(tmpidxs))[2:end-1]
end

You can clean that up or put it in a function if you want. 如果需要,您可以清理它或将其放入函数中。 It's an odd assumption though to assume that between each known value it's supposed to be linearly spaced. 虽然假设在每个已知值之间它应该是线性间隔的,但这是一个奇怪的假设。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM