【问题标题】:How can I define and initialize a numpy matrix of tuples?如何定义和初始化一个 numpy 元组矩阵?
【发布时间】:2018-10-27 01:46:40
【问题描述】:
M = np.zeros((N,L))

上面的语句给出了一个初始填充为 0 的 N x L 矩阵。我需要一个类似的语句来创建一个 N x L em> 矩阵填充有(0,0) 元组。如何创建它?

【问题讨论】:

  • 改为创建 3D 数组:M = np.zeros((N,L,2))?
  • 为什么不直接使用 3D 数组?
  • This 可能会有所帮助

标签: python python-3.x numpy


【解决方案1】:

这个solution怎么样:

N = 3
L = 3
M = np.zeros((N,L))
res =  np.array(list(zip(M.ravel(),M.ravel())), dtype=('i4,i4')).reshape(M.shape)

# returns
array([[(0, 0), (0, 0), (0, 0)],
       [(0, 0), (0, 0), (0, 0)],
       [(0, 0), (0, 0), (0, 0)]], dtype=[('f0', '<i4'), ('f1', '<i4')])

【讨论】:

    猜你喜欢
    • 2014-09-25
    • 2016-10-14
    • 1970-01-01
    • 1970-01-01
    • 2018-01-15
    • 1970-01-01
    • 2021-12-10
    • 2014-06-28
    • 1970-01-01
    相关资源
    最近更新 更多