如何将不同的二维数组转移到更大的网格中的所需位置?

问题描述 投票:0回答:1

我有一个像这样的坐标列表

[(x1,y1),(x2,y2),(x3,y3),(x4,y4),……]

坐标不是int;它们看起来像这样,例如 -0.23423,我有一个 (60*60) 2D 数组列表,我必须按顺序将其转换为坐标的位置。这些数组包含临时单元格,目标是将这些数组转换为所需的坐标后,我可以估计所需位置的温度。我已经为此工作了几个小时,但还没有想出任何想法。任何帮助都会有益的。我该如何接近它?

python arrays numpy interpolation
1个回答
0
投票

如果我正确理解你的问题,你应该采用以下方法:首先,你的坐标可能与二维数组的索引不完全对齐,你可能需要使用插值。之后,您需要将坐标映射到索引并最终读取温度。

import numpy as np
from scipy.interpolate import RegularGridInterpolator

temp_grids = [np.random.rand(60, 60) for _ in range(3)]

x_bounds = (-1, 1)
y_bounds = (-1, 1)

def interpolate_temperature(grid, coords, x_bounds, y_bounds):
    x = np.linspace(x_bounds[0], x_bounds[1], num=grid.shape[1])
    y = np.linspace(y_bounds[0], y_bounds[1], num=grid.shape[0])
    interpolator = RegularGridInterpolator((y, x), grid)
    
    temperatures = interpolator(coords)
    return temperatures

num_coords = 60
np.random.seed(0) 
coords = [(np.random.uniform(x_bounds[0], x_bounds[1]), np.random.uniform(y_bounds[0], y_bounds[1])) for _ in range(num_coords)]

for i, grid in enumerate(temp_grids):
    temps = interpolate_temperature(grid, coords, x_bounds, y_bounds)
    print(f"Temperatures for grid {i}: {temps}")

这给出了

Temperatures for grid 0: [0.54245305 0.52026447 0.77492708 0.20882522 0.74119398 0.60696394
 0.18780141 0.50828594 0.65877006 0.67259859 0.31361302 0.39179298
 0.55490317 0.3660215  0.45343405 0.62739553 0.44932449 0.40560742
 0.65463676 0.47007423 0.31183949 0.68157186 0.64546918 0.32595233
 0.61357888 0.47558091 0.59209396 0.64813476 0.34307311 0.48339497
 0.63247781 0.72547234 0.44999332 0.68751558 0.53547822 0.48672498
 0.50142419 0.53136812 0.56378821 0.65014139 0.60549947 0.71730996
 0.64960217 0.35205501 0.17622592 0.33931031 0.72108909 0.52844926
 0.49408864 0.31765803 0.77554765 0.18053496 0.79253244 0.29699311
 0.48063283 0.38537976 0.52785861 0.7529879  0.66795171 0.09471255]
Temperatures for grid 1: [0.36500183 0.40656738 0.72625848 0.48876747 0.29840103 0.51471031
 0.56824093 0.65885741 0.78624574 0.65095296 0.58219877 0.75467516
 0.2666243  0.65116363 0.47852513 0.36569874 0.68862314 0.59600088
 0.49252691 0.3129745  0.24725589 0.72643993 0.55324476 0.59143545
 0.53506604 0.58536069 0.67997756 0.64444727 0.66278467 0.40725802
...
 0.55411393 0.61361112 0.50698477 0.50338085 0.61805831 0.31806676
 0.7254115  0.67117083 0.3584695  0.4346187  0.30025535 0.3916735
 0.25748599 0.516774   0.81614513 0.17567274 0.46745756 0.47029596
 0.42986166 0.83020388 0.69958324 0.59300557 0.45240883 0.88804265]
© www.soinside.com 2019 - 2024. All rights reserved.