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]]]
2
  • Please post the expected outcome... Commented Apr 28, 2016 at 18:36
  • my bad, I'll try to include the output in a minute Commented Apr 28, 2016 at 18:43

3 Answers 3

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()])
...
Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

great I already have it in the form of a list, how can I turn it into a numpy array ? for further manupilation ?
Try looking here. I would use numpy.array(array1).
You may need to flatten the list first.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.