4

say I have array1 and it equals

array1=np.zeros((3,3))
that means [[0 0 0]
            [0 0 0]
            [0 0 0]]

but if I try the following it outputs an error:

array2=np.array[[111,222,333],[444,555,666],[77
array1[1,1]=array2

so how can I replace every single array1 element for a different new array ? for example

for i in range(3):
   for j in range (3):
       if i-j==0:
         array1[i,j]=array2

so it will become 3*9 instead of 3*3 ?

edit1 :expected output for the example above

  [[[251, 123, 584],
    [251, 123, 584],
    [251, 123, 584]],

   [[251, 123, 584],
    [251, 123, 584],
    [251, 123, 584]],

   [[251, 123, 584],
    [251, 123, 584],
    [251, 123, 584]]]

3 Answers3

1

If you can use lists instead of numpy arrays, you could do this:

array1 = [[0,0],[0,0]]
array2 = [[1,2],[3,4]]

for i in range(len(array1)):
    for j in range(len(array1[0])):
        array1[i][j] = array2

print array1

Try it online

If you must use numpy arrays, you could convert them to lists, then convert them back into numpy arrays after doing the above.

mbomb007
  • 3,788
  • 3
  • 39
  • 68
1

You cannot change the size (number of elements) of a numpy array. But you could use lists as intermediate step to create that final array:

>>> import numpy as np
>>> array1 = np.zeros((3,3))
>>> array2 = [251,123,584]
>>> np.array([[array2 for _ in row] for row in array1.tolist()])
array([[[251, 123, 584],
        [251, 123, 584],
        [251, 123, 584]],

       [[251, 123, 584],
        [251, 123, 584],
        [251, 123, 584]],

       [[251, 123, 584],
        [251, 123, 584],
        [251, 123, 584]]])

With some intermediate steps:

# Convert the original array to a list
>>> array1.tolist()
[[1.0, 2.0, 3.0], [1.0, 2.0, 3.0], [1.0, 2.0, 3.0]]

# Iterate over all elements and replace the element by array2:
>>> [[array2 for _ in row] for row in array1.tolist()]
[[[251, 123, 584], [251, 123, 584], [251, 123, 584]],
 [[251, 123, 584], [251, 123, 584], [251, 123, 584]],
 [[251, 123, 584], [251, 123, 584], [251, 123, 584]]]

# Convert the list to a numpy array again
>>> np.array([[array2 for _ in row] for row in array1.tolist()])
...
MSeifert
  • 145,886
  • 38
  • 333
  • 352
1

If you start with an array that is large enough, you can insert a smaller array. Broadcasting will take of replicating to match the target array.

Your expected result was 3d, not (9,9):

In [118]: a=np.zeros((3,3,3),int)

In [119]: a2=np.array([251,123,584])

In [120]: a[...]=a2    # short hand for a[:,:,:]

In [121]: a
Out[121]: 
array([[[251, 123, 584],
        [251, 123, 584],
        [251, 123, 584]],

       [[251, 123, 584],
        [251, 123, 584],
        [251, 123, 584]],

       [[251, 123, 584],
        [251, 123, 584],
        [251, 123, 584]]])

Here a[...]=a2 is shorthand for a[:,:,:]=a2[None,None,:]

Using a2[None,:,None] or a2[:,None,None] will replicate the a2 values in different dimensions.

np.tile(a2,[3,3,1]) also works. So does np.resize(a2,(3,3,3)). It may be trickier to generalize these.


Testing can be clearer if you make the dimensions different:

In [139]: a2=np.array([1,2,3,4])

In [140]: a[...]=a2

In [141]: a
Out[141]: 
array([[[ 1.,  2.,  3.,  4.],
        [ 1.,  2.,  3.,  4.],
        [ 1.,  2.,  3.,  4.]],

       [[ 1.,  2.,  3.,  4.],
        [ 1.,  2.,  3.,  4.],
        [ 1.,  2.,  3.,  4.]]])

Here only a 4 element a2 will fit along the last dimension.

hpaulj
  • 221,503
  • 14
  • 230
  • 353