2D numpy中的所有二元组合
我正在尝试创建一个代码,该代码可以在不包括旋转的Numpy矩阵中生成所有可能的组合。 我是什么意思旋转?我的意思是,以下矩阵数据实际上是相同的,因为它具有相同的数据,但旋转了90º,180º和270º。
我一直在尝试以下代码,但我认为我没有得到所有组合,并且其中一些缺少。您是否知道这是正确的方法?您知道是否有更有效的方法?
例如,考虑dx = 3和dy = 3
import numpy as np
def myfunction(data)
print(data)
dx=3
dy=3
data = np.zeros([dx, dy], dtype=np.uint8)
y=0
for x in range(dx):
for r in range(2):
data[x, y] = r
myfunction(data)
for y in range(dy):
for s in range(2):
data[x, y] = s
myfunction(data)
非常感谢!
I am trying to create a code that could generate all possible combinations of 0 and 1 in a numpy matrix excluding rotations.
What do I mean rotation? I mean that below matrix data are in fact the same because it has the same data but rotated 90º, 180º and 270º.
I have been trying below code but I think i am not getting all combinations and I have some of them missing. Do you have any idea if this is the right way? Do you know if there is any more efficient way?
For example, considering dx=3 and dy=3
import numpy as np
def myfunction(data)
print(data)
dx=3
dy=3
data = np.zeros([dx, dy], dtype=np.uint8)
y=0
for x in range(dx):
for r in range(2):
data[x, y] = r
myfunction(data)
for y in range(dy):
for s in range(2):
data[x, y] = s
myfunction(data)
Thank you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个矢量化解决方案,可以适用于小矩阵尺寸;代码中的评论说明了3乘3的情况:
主要思想是,我们可以将n b n b n binary矩阵的“点产品”与n by n矩阵一起使用,该矩阵由n矩阵组成,该矩阵由2的第一个n^2 powers组成(我称为后者得分矩阵)。当我们这样做时,我们将为每个可能的二进制矩阵获得一个唯一的整数。
为了说明旋转,我们可以使用“分数”矩阵4旋转的“点产品”,并将每个二进制矩阵与四个结果的最小值相关联。我们可以将此最小值称为二进制矩阵的分数。
最后,我们可以使用
np.nique
为每个唯一分数选择一个矩阵。例如,这是n = 2的输出:作为一项理智检查,二进制矩阵的数量直至旋转,此OEIS序列 n最多5:
Here is a vectorized solution that can work for small matrix sizes; comments in the code illustrate a 3 by 3 case:
The main idea is that we can take a "dot product" of an N by N binary matrix with an N by N matrix consisting of first N^2 powers of 2 (I call the latter the score matrix). When we do this, we get a unique integer for each possible binary matrix.
To account for rotations, we can take a "dot product" with 4 rotations of the "score" matrix, and associate each binary matrix to the minimum of the four results. We can refer to this minimum as a score of a binary matrix.
Finally, we can pick a matrix for each unique score with
np.unique
. For example, here is the output for n = 2:As a sanity check, the number of binary matrices up to rotation lines up with this OEIS sequence for n up to 5:
我不确定我的答案是否是最有效的方法,但是无论如何。
我们可以使用
numpy.rot90
函数旋转每个矩阵。让我们创建两个不同的词典,一个用于存储主矩阵,另一个用于存储主矩阵旋转,因此我们可以验证新矩阵是否只是先前发现的矩阵的旋转。
I am not sure if my answer is most efficient way, but anyways.
We can rotate each matrix using
numpy.rot90
function.Lets Create two different dictionaries, one to store the main matrices, the other one to store main matrices' rotations, so we can verify if a new matrix is just a rotation of previously found matrices.