简体   繁体   中英

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 ). I have tried the Interpolations ( https://github.com/tlycken/Interpolations.jl ) library, but I am having several issues in creating a working script.

I have a DataArrays.DataArray{Float64,1} with two points (let's say 1.5 and 10.5) and 5 NA between them:

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. In Julia, the interpolate function allows cubic interpolations. However, it seems not to be working with NAs. 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. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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