简体   繁体   English

将随机数据插值到规则网格

[英]interpolating random data to regular grid

My data looks like this 我的数据看起来像这样 数据

I want to interpolate this to a 4 cell grid. 我想将其插值到4单元格。 Each cell would just have average values of all the points lying inside it. 每个像元仅具有其内部所有点的平均值。 在此处输入图片说明

The output then should look like this 然后输出应如下所示

在此处输入图片说明

Thus we have converted the entire data to a 2x2 matrix. 因此,我们已将整个数据转换为2x2矩阵。 Each cell of this matrix will have average x coordinate & average y coordinate values of all the points lying inside them. 该矩阵的每个像元将具有位于它们内部的所有点的平均x坐标和平均y坐标值。

A1= (3,-3) ; A1 =(3,-3); A2 = (3.5, 1.5) A2 =(3.5,1.5)

A3= (-1,-3) ; A3 =(-1,-3); A4= (-2,1) A4 =(-2,1)

=====WHAT IVE TRIED===== =====什么尝试=====

avg = [[
        (
            ( mat[row][col][0]
            + mat[row][col+1][0]
            + mat[row+1][col][0]
            + mat[row+1][col+1][0] ) / 4.0
        , 
            ( mat[row][col][1]
            + mat[row][col+1][1]
            + mat[row+1][col][1]
            + mat[row+1][col+1][1] ) / 4.0
        )
        for col in range(0, len(mat[0]), 2) ]
    for row in range(0, len(mat), 2)
]

I'm not that good with numpy/scipy, i think this could be vastly improved in terms of elegancy and efficiency, but it works: 我对numpy / scipy不太满意,我认为这可以在优雅和效率方面得到极大的改善,但是它可以:

-> jupyter notebook with intermediate plots -> Jupyter笔记本与中间地块

Final code: 最终代码:

import numpy as np
import matplotlib.pyplot as plt
import math
data = np.random.uniform(low=-2.0, high=2.0, size=(2,100))
dataX = data[0]
dataY = data[1]

#plot the data
plt.plot(data[0], data[1], 'b+')

gridSize = 1.0

# grid coordinates are lower left point of grid rectangles
gridMaxX = math.floor(max(dataX) / gridSize)
gridMaxY = math.floor(max(dataY) / gridSize)
gridMinX = math.floor(min(dataX) / gridSize)
gridMinY = math.floor(min(dataY) / gridSize)

gridX = np.arange(gridMinX,gridMaxX + gridSize, gridSize)
gridY = np.arange(gridMinY,gridMaxY + gridSize, gridSize)

#plot the grid
for ix, x in enumerate(gridX):
    plt.axvline(x=x)
for iy, y in enumerate(gridY): 
    plt.axhline(y=y)

#iterate the grid
for gridPosX in gridX:
    for gridPosY in gridY:
        inCell = lambda x,y: (gridPosX<x and x<gridPosX+gridSize 
                              and gridPosY<y and y<gridPosY+gridSize)

        pointsInCell = [ (x,y) for (x,y) in zip(dataX, dataY) if inCell(x,y)]
        if len(pointsInCell) > 0:
            xPos, yPos = zip(*pointsInCell)
            plt.plot(np.mean(xPos), np.mean(yPos), 'ro')
plt.show()

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

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