2013-05-06 41 views
2
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]]) 
+0

可能有更有效的方法來設置這些值,而不是使用嵌套循環。如果你能解釋一下你的輸出是什麼意思,這將有所幫助。 – askewchan 2013-05-06 15:39:21

回答

0

這很難說什麼你的代碼試圖做。您希望的輸出應該是scindex

或者,也許你想創建一個新的數組,我會打電話給它output,然後你可以使用在s設置output價值coutput[s] = c

如果您事先不知道大小,那麼現在我能想到的最好的方法是跟蹤rowscols列表中的所有索引值以及列表中的實際值values

import numpy as np 
dx = 8 
dy = 10 
bx = 5.34 
by = 1.09 
index = np.zeros(dx+dy,dtype = 'int32') 
rows = [] 
cols = [] 
vals = [] 
for i in np.arange(2,dy+1): 
    for j in np.arange(1,dx+1): 
     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 
       rows.append(r-1) 
       cols.append(index[r]-1) 
       vals.append(i+(j-1)*dy) 

outshape = max(rows)+1, max(cols)+1 # now you know the size 
output = np.zeros(outshape, np.int) 
output[rows, cols] = vals 

然後,output看起來是這樣的:

In [60]: output 
Out[60]: 
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]]) 

我如果您事先知道尺寸:

import numpy as np 
dx = 8 
dy = 10 
bx = 5.34 
by = 1.09 
index = np.zeros(dx+dy,dtype = 'int32') 
outshape = (nrows, ncols)      # if you know the size 
output = np.zeros(outshape, np.int)    # initialize the output matrix 
for i in np.arange(2,dy+1): 
    for j in np.arange(1,dx+1): 
     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[r-1, index[r]-1] = i+(j-1)*dy # no need to set `s` or `c` 
+0

@ askewchan - 嗨,你解決的方法是給出正確的輸出。但我不知道輸出的大小。輸出大小實際上取決於(r,index [r])。在那種情況下,我怎樣才能定義輸出數組,以便輸出取決於r,index [r]的指數值。 – user2095624 2013-05-06 18:14:14

+0

所以實際上在這個代碼中r是半徑在20度之內(由theta <10定義),index是計算有多少半徑在那裏,輸出應該將索引放置在一個矩陣中。 – user2095624 2013-05-06 18:18:11

+0

有沒有什麼辦法可以像output = np.zeros(r,index [r])那樣設置輸出,以便它將創建輸出的確切大小並使用(r-1,index [r] -1)作爲索引並將i +(j-1)* dy的值放在輸出數組的相應索引位置,應該給出確切的輸出結果,正如你在回答中提到的那樣 – user2095624 2013-05-06 18:32:19