Skip to main content
 首页 » 编程设计

python之用每个可能的列创建一个矩阵

2025年05月04日15langtianya

如何制作一个由 0 和 1 值组成的 n x 2^n 矩阵,其中所有列都不同?例如,如果 n = 2 那将是

0011 
0101 . 

而且我可以使用 itertools 生成所有可能的元组。

list(itertools.product([0,1],repeat = 2)) 

但是我如何使这些列成为矩阵的列?

请您参考如下方法:

只需将 np.matrix 应用于您的结果:

>>> np.matrix(list(itertools.product([0,1],repeat = 2))) 
matrix([[0, 0], 
        [0, 1], 
        [1, 0], 
        [1, 1]])