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

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

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

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

另一方面,我有不同类型的数据,即二维数组(60*60)的列表。

使用温度单元格,我如何找到这个二维数组的中心位置并平移、传输或映射到更大网格中的坐标列表?

最好的方法是什么?

python arrays grid
1个回答
0
投票
  1. 确定二维数组的center
  2. 数组映射更大的网格
  3. 覆盖二维数组

实施:

import numpy as np
temperature_grid = np.random.rand(60, 60)
larger_grid = np.zeros((200, 200))
coordinates_list = [(100, 150), (50, 75), ...]
def overlay_grid(larger_grid, small_grid, center_x, center_y):
    small_grid_center_x, small_grid_center_y = small_grid.shape[1] // 2, small_grid.shape[0] // 2
    start_x = center_x - small_grid_center_x
    start_y = center_y - small_grid_center_y
    for i in range(small_grid.shape[0]):
        for j in range(small_grid.shape[1]):
            x, y = start_x + i, start_y +j
            if 0 <= y < larger_grid.shape[0] and 0 <= x < larger_grid.shape[1]:
                larger_grid[y, x] = small_grid[i, j]
center_x, center_y = coordinates_list[0]
overlay_grid(larger_grid, temperature_grid, center_x, center_y)
© www.soinside.com 2019 - 2024. All rights reserved.