Carlos Eduardo Corpus
Carlos Eduardo Corpus

Reputation: 349

Working with corresponding 3D numpy arrays in for loop

I was wondering what is the best way to work with corresponding 3D array. So I created 3 different numpy arrays, like this (this is just an example, the 3D array can be bigger), what I'm trying to do is replace some "sections" in each array of arr by the values in arr2 and arr3. For example in my code I want to replace arr[0, 2:] (the row section with the ones) by the values in arr2 and then the section arr[2:, 1] (the column section with the ones) by the values in arr3 I was thinking in using zip but I'm having trouble, my code is the next

import numpy as np

arr = np.array([[[0., 1., 1., 1., 1.],
                 [0., 0., 0., 0., 0.],
                 [0., 1., 0., 1., 0.],
                 [0., 1., 1., 0., 1.],
                 [0., 1., 0., 1., 0.]],

                [[0., 1., 1., 1., 1.],
                 [0., 0., 0., 0., 0.],
                 [0., 1., 0., 0., 1.],
                 [0., 1., 0., 0., 1.],
                 [0., 1., 1., 1., 0.]]])

arr2 = np.array([[[43, 25, 21],
                  [28, 43, 28]],

                 [[38, 29, 46],
                  [48, 27, 33]]])

arr3 = np.array([[[43, 43, 45],
                  [28, 24, 38]],

                 [[32, 26, 30],
                  [40, 23, 20]]])

for i, j in zip(arr, arr2):
    for k in j:
        i[0, 2:] = k

print(i) 

#output
#[[ 0.  1. 48. 27. 33.]
# [ 0.  0.  0.  0.  0.]
# [ 0.  1.  0.  0.  1.] #The last array in arr with the last array in arr2
# [ 0.  1.  0.  0.  1.] #It does almost what I need, but not quite
# [ 0.  1.  1.  1.  0.]]

#If I try using append like

aux = []

for i, j in zip(arr, arr2):
    for k in j:
        aux.append(i[0, 2:] = k)

#I get an error
#keyword can't be an expression


The problem is not only that, but also, I have 3 different arrays, so I don't want to create an unnecessary list after I finish replacing the row section and then use this list to replace the column section if there is a more simple way, my desired output is the next

 newarr =      [[[0.,  1., 43., 25., 21.],
                 [0.,  0.,  0.,  0.,  0.],
                 [0., 43.,  0.,  1.,  0.], 
                 [0., 43.,  1.,  0.,  1.],
                 [0., 45.,  0.,  1.,  0.]],
                                                
                [[0.,  1., 28., 43., 28.],
                 [0.,  0.,  0.,  0.,  0.],
                 [0., 28.,  0.,  1.,  0.],
                 [0., 24.,  1.,  0.,  1.],
                 [0., 38.,  0.,  1.,  0.]],

                [[0.,  1., 38., 29., 46.],
                 [0.,  0.,  0.,  0.,  0.],
                 [0., 32.,  0.,  0.,  1.],
                 [0., 26.,  0.,  0.,  1.],
                 [0., 30.,  1.,  1.,  0.]],

                [[0.,  1., 48., 27., 33.],
                 [0.,  0.,  0.,  0.,  0.],
                 [0., 40.,  0.,  0.,  1.],
                 [0., 23.,  0.,  0.,  1.],
                 [0., 20.,  1.,  1.,  0.]]]


So the first 2D array in arr duplicate itself and then replaces the values in the first 2D arrays of arr2 and arr3 and do the same for the second 2D array in arr and the second 2d arrays in arr2 and arr3, if there is any way you can point me to the right direction I will be grateful, thank you!

Upvotes: 2

Views: 73

Answers (1)

Divakar
Divakar

Reputation: 221514

A simpler way would be -

out = arr.repeat(2,axis=0)
out[:,0,2:] = arr2.reshape(-1,arr2.shape[2])
out[:,2:,1] = arr3.reshape(-1,arr3.shape[2])

Upvotes: 2

Related Questions