2017-12-18 6 views
1

청크를 다른 행렬의 일부로 복사하려고합니다. 모든 종류의 n 차원 배열에 이것을 사용하려면 [] 연산자를 통해 오프셋이있는 목록을 적용해야합니다. 이것을 할 수있는 방법이 있습니까?파이썬 : 목록을 사용하여 [from : to] 임의 numpy 배열

mat_bigger[0:5, 0:5, ..] = mat_smaller[2:7, 2:7, ..] 

같은 :

off_min = [0,0,0] 
off_max = [2,2,2] 
for i in range(len(off_min)): 
    mat_bigger[off_min[i] : off_max[i], ..] = .. 
+0

그래 내가 NumPy와 – dgrat

+0

확인이 https://stackoverflow.com/questions/26506204/replace-sub-part-of-matrix-by-another-small-matrix-in-numpy를 사용합니까 – AndreyF

+0

또는 [this] (https://stackoverflow.com/a/47605511/7207392) –

답변

2

당신은 slice 객체의 튜플을 생성하여이 작업을 수행 할 수 있습니다. 예를 들어 :

mat_big = np.zeros((4, 5, 6)) 
mat_small = np.random.rand(2, 2, 2) 

off_min = [2, 3, 4] 
off_max = [4, 5, 6] 

slices = tuple(slice(start, end) for start, end in zip(off_min, off_max)) 

mat_big[slices] = mat_small