私は次のコードを持っています:
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]]
私の問題(視野)に合わせてかなりカスタマイズされているので、答えは大きく異なります。
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
解決策は、最大行、中央の列から右に向かって境界線上で変更する値の数に依存し、次に2つの方向に同時に移動を開始します。複雑な解決策については申し訳ありませんが、@ Ophirに言ったように、それは私の問題に対してかなりカスタマイズされた解決策です。(私が質問をするとき、私はそれを単純化するために非常に一般的なフォーラムを使用しました。)これがいつか他の人を助けることを願っています。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加