2017-08-27 2 views
3

Je voudrais insérer un tableau de taille 2 * 2 rempli de zéros sur un tableau plus grand. En outre, je voudrais déplacer la position du tableau zéro de gauche à droite, de haut en bas de manière itérative.Insérer un tableau prédéfini sur un grand tableau et décaler de façon itérative la position du tableau plus petit

zero_array =[0 0 
      0 0] 

large_array =[ 1 2 3 4 
       5 6 7 8 
       9 10 11 12 
       13 14 15 16] 

résultat requis:

Ist iteration 
      [ 0 0 3 4 
       0 0 7 8 
       9 10 11 12 
       13 14 15 16] 
2nd iteration 
      [ 1 0 0 4 
       5 0 0 8 
       9 10 11 12 
       13 14 15 16] 
3rd iteration 
      [ 1 2 0 0 
       5 6 0 0 
       9 10 11 12 
       13 14 15 16] 

4th Iteration 
      [ 1 2 3 4 
       0 0 7 8 
       0 0 11 12 
       13 14 15 16] 

Et ainsi de suite ...

Répondre

0
import copy 
import numpy as np 
la=np.array(<insert array here>) 
za=np.zeros((2,2)) 
ma=copy.deepcopy(la) 
for i in range(len(la)-len(za)+1): 
    for j in range(len(la)-len(za)+1): 
     la=copy.deepcopy(ma) 
     la[i:i+len(za),j:j+len(za)]=za 
     print la 
#la=large array 
#za=zero array 
0

Insertion d'un tableau plus petit dans un plus grand tableau est assez facile, il vous suffit de savoir où il doit être inséré (par exemple par la coordonnée en haut à gauche):

def insert_array(bigger_arr, smaller_arr, pos): 
    out = np.array(bigger_arr, copy=True) 
    if bigger_arr.ndim != smaller_arr.ndim or bigger_arr.ndim != len(pos): 
     raise ValueError('incompatible dimension') 
    slicer = [slice(p, p+extend) for p, extend in zip(pos, smaller_arr.shape)] 
    out[tuple(slicer)] = smaller_arr 
    return out 

>>> insert_array(np.arange(16).reshape(4, 4), np.zeros((2, 2)), pos=(0, 0)) 
array([[ 0, 0, 2, 3], 
     [ 0, 0, 6, 7], 
     [ 8, 9, 10, 11], 
     [12, 13, 14, 15]]) 

Alors tout ce qui reste est de calculer les coordonnées dans une boucle. Cela pourrait se faire facilement avec une fonction de générateur:

from itertools import product 
def looping_inserting(bigger_arr, smaller_arr): 
    for startpos in product(*[range(big_dim - small_dim + 1) 
           for big_dim, small_dim 
           in zip(bigger_arr.shape, smaller_arr.shape)]): 
     yield insert_array(bigger_arr, smaller_arr, startpos) 

for newarr in looping_inserting(np.arange(1, 17).reshape(4, 4), np.zeros((2, 2))): 
    print(newarr) 

qui imprime:

[[ 0 0 3 4] 
[ 0 0 7 8] 
[ 9 10 11 12] 
[13 14 15 16]] 
[[ 1 0 0 4] 
[ 5 0 0 8] 
[ 9 10 11 12] 
[13 14 15 16]] 
[[ 1 2 0 0] 
[ 5 6 0 0] 
[ 9 10 11 12] 
[13 14 15 16]] 
[[ 1 2 3 4] 
[ 0 0 7 8] 
[ 0 0 11 12] 
[13 14 15 16]] 
[[ 1 2 3 4] 
[ 5 0 0 8] 
[ 9 0 0 12] 
[13 14 15 16]] 
[[ 1 2 3 4] 
[ 5 6 0 0] 
[ 9 10 0 0] 
[13 14 15 16]] 
[[ 1 2 3 4] 
[ 5 6 7 8] 
[ 0 0 11 12] 
[ 0 0 15 16]] 
[[ 1 2 3 4] 
[ 5 6 7 8] 
[ 9 0 0 12] 
[13 0 0 16]] 
[[ 1 2 3 4] 
[ 5 6 7 8] 
[ 9 10 0 0] 
[13 14 0 0]]

Beaucoup de ce code pourrait être simplifié si vous ne traitez avec des tableaux 2D, peut-être c'est aussi mieux comprendre:

from itertools import product 

def insert_array(bigger_arr, smaller_arr, pos): 
    out = np.array(bigger_arr, copy=True) 
    if bigger_arr.ndim != smaller_arr.ndim or bigger_arr.ndim != len(pos): 
     raise ValueError('incompatible dimension') 
    out[pos[0]: pos[0] + smaller_arr.shape[0], 
     pos[1]: pos[1] + smaller_arr.shape[1]] = smaller_arr 
    return out 

def looping_inserting(bigger_arr, smaller_arr): 
    for pos0 in range(bigger_arr.shape[0] - smaller_arr.shape[0] + 1): 
     for pos1 in range(bigger_arr.shape[1] - smaller_arr.shape[1] + 1): 
      yield insert_array(bigger_arr, smaller_arr, (pos0, pos1)) 

for newarr in looping_inserting(np.arange(1, 17).reshape(4, 4), np.zeros((2, 2))): 
    print(newarr)