简体   繁体   English

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

[英]Classifying dots in matrix (Python)

I have big matrix, like 600x600 with 9 dots in 9 same sectors(# like tic-tac-toe). 我有一个大矩阵,例如600x600,在9个相同的扇区中有9个点(#像井字游戏一样)。 I need to turn it to 3x3 array with iDs of dots in this sectors, like: [[id2,id1,id5],[id4,id6,id7],[id3,id8,id9]] Dividing plane in 9 small planes goes really bad. 我需要将其转换为3x3阵列,并在该扇区中添加点的iD,例如:[[id2,id1,id5],[id4,id6,id7],[id3,id8,id9]]在9个小平面中划分平面特别糟糕。 I need something like relative positions, and dont know even the worlds I need to google 我需要类似相对位置的东西,甚至都不知道我需要Google搜索的世界

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]

Following from the comments above. 从上面的评论。 This is still asking for clarification but shows a way of interpreting your question. 这仍然需要澄清,但显示了一种解释您的问题的方法。

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)

Can you use it as a basis to explain what you expect to see? 您可以以此为基础来解释您期望看到的内容吗? If your input data was only 6 by 6 what would it look like and what would you expect to see coming out? 如果您的输入数据只有6 x 6,它将是什么样子,您希望看到什么?

Edits: Two typos corrected. 编辑:纠正了两个错字。

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

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