【发布时间】:2014-04-08 13:48:28
【问题描述】:
我有数组newx 和newy,大小分别为nx*ns 和ny*ns,其中nx!=ny。
我希望能够通过以下方式在数组f 中设置newx 和newy 定义的元素:
f = np.zeros([nx,ny,ns])
for s in range(ns):
f[newx[:,s],newy[:,s],s] = s
不幸的是,这给出了一个错误:
ValueError: shape mismatch: objects cannot be broadcast to a single shape
我理解错误,但我终生无法找出正确的语法。请帮忙。
编辑:提供示例代码:
import numpy as np
newx = np.array([[0,1],
[1,2],
[2,3],
[3,0]])
newy = np.array([[0,1],
[1,2],
[2,0]])
f = np.zeros([4,3,2])
for s in range(2):
f[newx[:,s],newy[:,s],s] = s
【问题讨论】:
-
向我们展示完整的工作代码。
-
newx 和 newy 没有任何意义。如果你想将某个坐标的元素设置为 s,你应该有相同数量的 x,y,s 三元组,而不是不同数量的 x,s 和 y,s 对。