【问题标题】:Access element of ndarray with shape=(dim,L,2)使用 shape=(dim,L,2) 访问 ndarray 的元素
【发布时间】:2020-02-17 08:41:16
【问题描述】:

我目前正在使用 numpy.ndarray。我初始化了一个多维矩阵:

phi = np.zeros(dim_tuple(dim,L)+(2,))

其中 dim_tuple(dim,L) 返回具有昏暗时间 L 的元组 (L,L, ... ,L)。

我想创建一个作为参数的函数:

  • 长度为 dim 的数组 x
  • 一个数字 N:0 或 1

这将更新矩阵 phi 的元素 [x[0],x[1], ..., x[dim],N] 的值。

从现在开始,我尝试了这个:

index = np.append(x,N)

phi[index]=new_value

除了它不起作用。事实上,我检查它是否返回了这个命令:

phi[[x[0],x[1],...,x[dim],N]] = new_value

我试图在互联网上找到答案,但没有成功。

感谢您的帮助!

杰雷米

【问题讨论】:

  • 请添加示例输出。我建议可能是 3 x 3 x 3 x 2。
  • 我也相信dim_tuple(dim,L) 只是(L,) * dim
  • 谢谢你的回答 :) 你说得对

标签: python list function numpy multidimensional-array


【解决方案1】:

我想你想要的是:

phi[tuple(x) + (N,)] = new_value

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    • 2020-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    相关资源
    最近更新 更多