在矩阵中对点进行分类(Python)



我有一个大矩阵,比如600x600,在9个相同的扇区中有9个点(#like tic-tac-toe(。我需要把它变成3x3阵列,在这个扇区中有点的ID,比如:[[id2,id1,id5],[id4,id6,id7],[id3,id8,id9]把飞机分成9个小飞机真的很糟糕。我需要一些类似相对位置的东西,甚至不知道我需要谷歌的世界

def classificator(val):
global A
global closed
height, width = map(int, closed.shape)
h1 = height // 3
w1 = width // 3
h2 = height // 3 * 2
w2 = width // 3 * 2
for x in range(len(val)):
xcoord = val[x][0]
ycoord = val[x][1]
if 0 <= val[x][0] < h1 and 0 <= val[x][1] < w1 and A[0, 0] == '_': #top left X
A[0, 0] = val[x][2]

以下是上面的注释。这仍然是要求澄清,但显示了一种解释你的问题的方式。

In [1]: import numpy as np
In [2]: data_in=np.fromfunction(lambda r, c: 10*r+c, (6, 6))
# Create an array where the vales give a indication of where they are in the array.
In [3]: data_in
Out[3]: 
array([[ 0.,  1.,  2.,  3.,  4.,  5.],
[10., 11., 12., 13., 14., 15.],
[20., 21., 22., 23., 24., 25.],
[30., 31., 32., 33., 34., 35.],
[40., 41., 42., 43., 44., 45.],
[50., 51., 52., 53., 54., 55.]])
In [4]: slices=[np.s_[0:3], np.s_[3:6] ]
In [5]: slices
Out[5]: [slice(0, 3, None), slice(3, 6, None)]
In [8]: result=np.zeros((4,3,3), dtype=np.int32)
In [9]: ix=0
In [12]: for rows in slices:
...:     for columns in slices:
...:         result[ix,:,:]=data_in[rows, columns]
...:         ix+=1
...:         
In [13]: result
Out[13]: 
array([[[ 0,  1,  2],
[10, 11, 12],    # Top Left in data_in
[20, 21, 22]],
[[ 3,  4,  5],
[13, 14, 15],    # Top Right in data_in            
[23, 24, 25]],
[[30, 31, 32],
[40, 41, 42],    # Bottom Left in data_in
[50, 51, 52]],
[[33, 34, 35],
[43, 44, 45],    # Bottom Right in data_in
[53, 54, 55]]], dtype=int32)

你能以此为基础来解释你期望看到什么吗?如果你的输入数据只有6乘6,它会是什么样子,你希望看到什么?

编辑:更正了两个打字错误。

最新更新