【发布时间】:2019-11-21 06:32:56
【问题描述】:
我的代码中有两个嵌套的 for 循环,但一个有效,另一个无效,它们也是相同的,它在我的脑海中发挥作用
此代码有效
def nearest(furthestpoints,lastpoints,lastIDs,currentpoints,img,threshold = 30):
currentIDs = np.empty(0,dtype = float)
normList = np.empty(0,dtype = float)
for i in range(0,len(currentpoints)):
for a in range(0,len(lastpoints)):
normList = np.append(normList,np.linalg.norm(currentpoints[i,:] - lastpoints[a,:]))```
但是这段代码会产生错误
File "/home/oliver/tracker.py", line 22, in getLostPoints
normList = np.append(normList,np.linalg.norm(current[i,:] - last[a,:]))
TypeError: range indices must be integers or slices, not tuple
def getLostPoints(last,lastIDs,current,threshold = 50):
normList = np.empty(0,dtype = float)
goodIDs = np.empty(0,dtype = int)
lostPoints = np.empty((2,0),dtype = float)
for i in range(0,len(last)):
for a in range(0,len(current)):
normList = np.append(normList,np.linalg.norm(current[i,:] - last[a,:]))
if min(normList) < threshold:
indexMin = np.argmin(normList)
goodIDs = np.append(goodIDs,lastIDs[indexMin])
normList = np.empty(0,dtype = float)
这些只是循环,这段代码还有更多内容
输入数组的形状为(n,2)
其中n 是点数
【问题讨论】:
-
您的切片是:current[i,:] - last[a,:]。你的意思是他们是: current[i:] - last[a:] 没有逗号。
-
这不起作用,因为我试图索引的数组是形状 (5,2) 你建议的会产生广播错误 ValueError: 操作数不能与形状一起广播 (5,) (5, 2)
标签: python python-3.x numpy