从np.zeros数组创建单个数组,使用原始矩阵的每个单独行和列的数组

时间:2018-01-27 16:16:54

标签: python arrays numpy for-loop numpy-slicing

我有一个由6行×6列字母数字字符组成的屏幕(例如p300 speller)我想为每个可能的行和列创建一个数组(它们总共为12个)。当特定的列/行被照亮时,用1填充它,而矩阵的其余部分用零填充。最后,我想将特定矩阵与屏幕上发生的时间事件相关联。

分别切片行/列,为所需的12个输出生成变量。

event_mtx = np.zeros((6,6), dtype=np.int)
_event_mtx = np.zeros((6,6), dtype=np.int)
#replace first row with ones
event_mtx[0]=1
# or replace last column with ones
_event_mtx[:,5]=1

python和编码的新手,循环应该有助于使这更容易。以下是我尝试的行。为每一行生成一个,但是前一行仍然用1填充,并在每次迭代时继续构建,用1填充6x6数组。创建" new_event"传递当前迭代的数组似乎也无济于事。如何在循环中生成单个数组而不构建前一个?思考/评论赞赏

event_mtx = np.zeros((6,6), dtype=np.int)
#new_event =np.zeros((6,6), dtype=np.int)

for i in range(len(event_mtx)):
    for j in range(len(event_mtx[j])):
        event_mtx[0+i] = 1
        #np.new_event = event_mtx

    print(event_mtx)
    #print(new_event)

1 个答案:

答案 0 :(得分:0)

以下是一些可能性:

>>> import numpy as np
>>> 
>>> rows = np.zeros((6, 6, 6), dtype=int)
>>> np.einsum('iij->ij', rows)[...] = 1
>>> cols = np.zeros((6, 6, 6), dtype=int)
>>> np.einsum('iji->ij', cols)[...] = 1
>>> 
>>> for block in (rows, cols):
...     for m in zip(*block):
...         print(list(map(''.join, map(map, 6*(str,), m))))
...     print()
... 
['111111', '000000', '000000', '000000', '000000', '000000']
['000000', '111111', '000000', '000000', '000000', '000000']
['000000', '000000', '111111', '000000', '000000', '000000']
['000000', '000000', '000000', '111111', '000000', '000000']
['000000', '000000', '000000', '000000', '111111', '000000']
['000000', '000000', '000000', '000000', '000000', '111111']

['100000', '010000', '001000', '000100', '000010', '000001']
['100000', '010000', '001000', '000100', '000010', '000001']
['100000', '010000', '001000', '000100', '000010', '000001']
['100000', '010000', '001000', '000100', '000010', '000001']
['100000', '010000', '001000', '000100', '000010', '000001']
['100000', '010000', '001000', '000100', '000010', '000001']
>>> 
>>> row0 = np.outer(0==np.arange(6), np.ones(6, dtype=int))
>>> rows =[np.roll(row0, i, axis=0) for i in range(6)]
>>> col0 = np.outer(np.ones(6, dtype=int), 0==np.arange(6))
>>> cols =[np.roll(col0, i, axis=1) for i in range(6)]
>>> 
>>> for block in (rows, cols):
...     for m in zip(*block):
...         print(list(map(''.join, map(map, 6*(str,), m))))
...     print()
... 
['111111', '000000', '000000', '000000', '000000', '000000']
['000000', '111111', '000000', '000000', '000000', '000000']
['000000', '000000', '111111', '000000', '000000', '000000']
['000000', '000000', '000000', '111111', '000000', '000000']
['000000', '000000', '000000', '000000', '111111', '000000']
['000000', '000000', '000000', '000000', '000000', '111111']

['100000', '010000', '001000', '000100', '000010', '000001']
['100000', '010000', '001000', '000100', '000010', '000001']
['100000', '010000', '001000', '000100', '000010', '000001']
['100000', '010000', '001000', '000100', '000010', '000001']
['100000', '010000', '001000', '000100', '000010', '000001']
['100000', '010000', '001000', '000100', '000010', '000001']
>>>
>>> rows =[np.add.outer(i==np.arange(6), np.zeros(6, dtype=int)) for i in range(6)]
>>> cols =[np.add.outer(np.zeros(6, dtype=int), i==np.arange(6)) for i in range(6)]
>>> 
>>> for block in (rows, cols):
...     for m in zip(*block):
...         print(list(map(''.join, map(map, 6*(str,), m))))
...     print()
... 
['111111', '000000', '000000', '000000', '000000', '000000']
['000000', '111111', '000000', '000000', '000000', '000000']
['000000', '000000', '111111', '000000', '000000', '000000']
['000000', '000000', '000000', '111111', '000000', '000000']
['000000', '000000', '000000', '000000', '111111', '000000']
['000000', '000000', '000000', '000000', '000000', '111111']

['100000', '010000', '001000', '000100', '000010', '000001']
['100000', '010000', '001000', '000100', '000010', '000001']
['100000', '010000', '001000', '000100', '000010', '000001']
['100000', '010000', '001000', '000100', '000010', '000001']
['100000', '010000', '001000', '000100', '000010', '000001']
['100000', '010000', '001000', '000100', '000010', '000001']

>>> rows = np.empty((6, 6, 6), dtype=int)
>>> rows[...] = np.identity(6)[..., None]
>>> cols = np.empty((6, 6, 6), dtype=int)
>>> cols[...] = np.identity(6)[:, None]
>>> 
>>> for block in (rows, cols):
...     for m in zip(*block):
...         print(list(map(''.join, map(map, 6*(str,), m))))
...     print()
... 
['111111', '000000', '000000', '000000', '000000', '000000']
['000000', '111111', '000000', '000000', '000000', '000000']
['000000', '000000', '111111', '000000', '000000', '000000']
['000000', '000000', '000000', '111111', '000000', '000000']
['000000', '000000', '000000', '000000', '111111', '000000']
['000000', '000000', '000000', '000000', '000000', '111111']

['100000', '010000', '001000', '000100', '000010', '000001']
['100000', '010000', '001000', '000100', '000010', '000001']
['100000', '010000', '001000', '000100', '000010', '000001']
['100000', '010000', '001000', '000100', '000010', '000001']
['100000', '010000', '001000', '000100', '000010', '000001']
['100000', '010000', '001000', '000100', '000010', '000001']