2016-08-01 91 views
3

我有以下代碼:墊numpy的陣列具有固定陣列恆定

x = np.array([[1]]) 
print x 
print np.lib.pad(x,(1,1),mode='constant',constant_values=[4,8]) 

輸出:

[[1]] 
[[4 4 8] 
[4 1 8] 
[4 8 8]] 

問題是:在我想把新填充的常數值 例如:

print np.lib.pad(x,(1,1),mode='constant',constant_values = [1,2,3,4,5,6,7,8]) 

和輸出等:

[[1,2,3] 
[8,1,4] 
[7,6,5]] 

回答

0

答案是不同的方式,因爲這是非常定製,以我的問題(視場)。

def updatevalues(self,array,elementsCount): 
    counter =0 
    R1 =Agent.GetCenterCoords(array.shape)[0] 
    C1 = array.shape[1]-1 
    coords = {'R1':R1,'R2':R1,'C1':C1,'C2':C1,'Phase':1} 
    array[coords['R1'],coords['C1']] = True 

    while counter<elementsCount: 
     counter +=2 
     self.Phases[coords['Phase']](array,coords) 

def Phase1(self,array,coords): 
    ''' 
    Phase 1. 
    During this phase we start from the max column(C1,C2) and middle Row (R1,R2) 
    and start moving up and down till 
    minimum row (R1) , max Row (R2) then we move to phase 2 
    ''' 
    coords['R1'] -=1 
    coords['R2'] +=1 
    array[coords['R1'],coords['C1']] = True 
    array[coords['R2'],coords['C2']] = True 
    if coords['R1']==0 or coords['R2'] == array.shape[0]-1: 
     coords['Phase']=2 

def Phase2(self,array,coords): 
    ''' 
    Phase 2. 
    During this phase we start from the max column (C1,C2) and Min,Max Rows (R1,R2) 
    and start changing (C1,C2 to minimum) till 
    C1,C2 ==0 then we move to phase 3 
    ''' 
    coords['C1'] -=1 
    coords['C2'] -=1 
    array[coords['R1'],coords['C1']] = True 
    array[coords['R2'],coords['C2']] = True 
    if coords['C1']==0 or coords['C2'] ==0: 
     coords['Phase']=3 

def Phase3(self,array,coords): 
    ''' 
    Phase 3. 
    During this phase we start from the minimum columns (C1,C2) and Min,Max Rows (R1,R2) 
    and start changing (R1,R2) toward center till R1==R2 then we break (all border got covered) 
    ''' 
    coords['R1'] +=1 
    coords['R2'] -=1 
    array[coords['R1'],coords['C1']] = True 
    array[coords['R2'],coords['C2']] = True 
    if coords['R1']==coords['R2']: 
     coords['Phase']=4 

@staticmethod 
def GetCenterCoords(shape): 
    return (shape[0]-1)/2 , (shape[1]-1)/2 

的解決方案取決於我們想要多少個值來改變從最高行開始的邊界,中間一列到右側,然後開始同時在兩個方向移動。 對不起,複雜的解決方案,但正如我告訴你@Ophir它是我的問題非常定製的解決方案。 (當我提出這個問題時,我使用了一個非常一般的論壇來簡化它。) 希望這有助於他人有一天。

1

這是Print two-dimensional array in spiral order逆向工程:

inner_array = np.array([3, 6, 7, 2]) 
outer_array = np.array([0, 23, 3, 5, 6, 8, 99, 73, 18, 42, 67, 88, 91, 12]) 

total_array = inner_array[::-1][np.newaxis] 
i = 0 
while True: 
    s = total_array.shape[1] 
    try: 
     total_array = np.vstack([total_array, outer_array[i:i+s]]) 
    except ValueError: 
     break 
    try: 
     total_array = np.rot90(total_array, -1) 
    except ValueError: 
     pass 
    i += s 
total_array = np.rot90(total_array, -1) 
print(total_array) 
+0

它不總是從1到8,大多數情況下它是二進制(0,1),所以任何組合都是可能的。我試過你的代碼,它不會維護提供的數組的順序。 和陣列我想填充不總是1個元素有時它不僅僅是這個。 – samer226047

+0

看到我編輯的答案 –

+0

感謝代碼工作正常。但我設法爲我的情況做了10^-5次的定製代碼,而用你的代碼做了10^-3。 再次感謝。 – samer226047