Julia:如何将非均匀间隔的二维数据插入到网格上?

标签 julia interpolation

我有一系列数据点(非均匀地)分布在二维网格中。我想将这些分散的数据点插入到一个统一的网格上。 Julia 中是否有方便的内置函数允许我执行此操作?或者我可以添加一个额外的包怎么样(我一直在查看 Interpolations.jl、Grid.jl 和 GridInterpolations.jl,但我不知道如何将它们用于此目的)?我正在寻找类似于 Matlab 的 griddata .这是一个示例(具有随机选择的值)来演示我正在寻找的内容:

# x and y position of known data points
x = [ 1.5 , 8.8 , 2.9 , 7.2 , 7.1 , 3.8 , 8.4 , 2.1 , 0.8 , 5.1 , 7.5 ]
y = [ 6.1 , 9.3 , 5.2 , 7.7 , 9.8 , 7.7 , 8.5 , 6.4 , 5.8 , 9.0 , 8.7 ]

# value of known data points
val = [ 153.9 , 211.8 , 443.6 , 370.8 , 233.8 , 307.2 , 580.3 , 440.9 , 322.2 , 109.3 , 190.8 ]

# x and y positions to describe the interpolation grid
x_interp = [ 0.5 , 2.5 , 4.5 , 6.5 , 8.5 , 10.5 ]
y_interp = [ 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 , 7.0 , 8.0 , 9.0 ]

# Some function to interpolate the scattered data onto the grid
val_grid = SomeInterpolationFunction(x,y,val,x_interp,y_interp)

Julia 中是否有能够执行此操作的函数?

最佳答案

我找到了一种可能的方法。我会在这里发布以防其他人遇到类似的问题。

using PyCall
@pyimport scipy.interpolate as si

# Some 2D function
f(x,y) = sin(x)*cos(y)

# Location of random points to sample the function at
np = 2500
xmin = 0.
xmax = 50.
ymin = 10.
ymax = 95.
x = xmin + xmax*rand(np)
y = ymin + ymax*rand(np)
points = [x y]

# Value of the function at the random points
val = zeros(np)
for ip = 1:np
    val[ip] = f(x[ip],y[ip])
end

# Create a uniform grid to interpolate onto
nx = 50
ny = 75
xgrid = collect(linspace(xmin,xmax,nx))
ygrid = collect(linspace(ymin,ymax,ny))
grid_x = kron(ones(ny),xgrid')
grid_y = kron(ygrid,ones(1,nx))

# Perform the interpolation
grid_val = si.griddata(points,val,(grid_x,grid_y),method="cubic")

关于Julia:如何将非均匀间隔的二维数据插入到网格上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41176260/

相关文章:

julia - 使用 ./startup.jl 文件设置 nprocs()

rotation - Julia AffineTransforms 旋转角的符号

types - 如何检查类型的可变性

interpolation - 二维插值不规则网格fortran

c++ - 基本插值、放大和缩小

algorithm - 在 Julia 中迭代具有固定总和的整数数组

dictionary - 如何在 Dict of Dicts 上使用 Julia map ?

matlab - Matlab绘图函数使用什么插值技术来显示数据?

python - scipy.interpolate.griddata : cut z-value and get area inside it

ruby-on-rails - 字符串插值不适用于 Ruby heredoc 常量