簡體   English   中英

在矩陣中對點進行分類(Python)

[英]Classifying dots in matrix (Python)

我有一個大矩陣,例如600x600,在9個相同的扇區中有9個點(#像井字游戲一樣)。 我需要將其轉換為3x3陣列,並在該扇區中添加點的iD,例如:[[id2,id1,id5],[id4,id6,id7],[id3,id8,id9]]在9個小平面中划分平面特別糟糕。 我需要類似相對位置的東西,甚至都不知道我需要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]

從上面的評論。 這仍然需要澄清,但顯示了一種解釋您的問題的方法。

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 x 6,它將是什么樣子,您希望看到什么?

編輯:糾正了兩個錯字。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM