import numpy as np
dx = 8
dy = 10
bx = 5.34
by = 1.09
index = np.zeros((dx+dy),dtype = 'int32')
for i in np.arange(1,dy+1):
for j in np.arange (1,dx+1):
if i-by > 0:
theta = 180*np.arctan(abs(j-bx)/(i-by))/np.pi
if theta<10:
r = np.around(np.sqrt((j-bx)**2+(i-by)**2))
r = r.astype(int)
if r>0:
index[r]+=1
output = np.zeros((r, index[r]),dtype='int32')
output[r-1,index[r]-1] = i+(j-1)*dy
該代碼應該使用(r,index [r])作爲索引並將i +(j-1)* dy的值放入相應的索引中,並將其記錄到新矩陣中/陣列,它應該像這 -Python numpy索引不起作用
array([[ 0, 0, 0],
[ 0, 0, 0],
[44, 0, 0],
[45, 55, 0],
[46, 56, 0],
[47, 57, 0],
[48, 58, 0],
[39, 49, 59],
[40, 50, 60]])
,但我有這樣的,而不是它的輸出我不want-
array([[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 60]])
可能有更有效的方法來設置這些值,而不是使用嵌套循環。如果你能解釋一下你的輸出是什麼意思,這將有所幫助。 – askewchan 2013-05-06 15:39:21