-2

I'm trying to add multiple arrays together and I'm stuck.

For example, I have those two arrays:

[[1 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 1 0 1]
 [0 0 1 0 0 0 0 1 0 0]
 [1 0 0 0 1 0 0 0 0 0]
 [1 1 0 0 0 0 0 0 2 0]
 [0 0 0 0 0 1 0 0 0 0]
 [0 0 0 1 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 1]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 2]]

and

[[1 0 0 0 0 0 0 0 0 0]
 [0 1 0 0 1 0 0 0 0 1]
 [0 0 1 0 0 0 0 0 0 0]
 [0 1 1 0 0 0 0 0 0 0]
 [0 0 0 0 2 0 0 0 0 0]
 [0 0 0 0 0 1 0 0 0 0]
 [1 0 0 0 0 0 1 0 1 0]
 [0 1 0 0 0 1 0 1 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]]

How do I do to add them together such that the resulting array will look like this:

element from the 1st row and from the 1st column (top left) in 1st array + element from the 1st row and from the 1st column (top left) in 2nd array = 2

So the element in the 1st row and 1st column in the resulting array will be 2 and so on for every element.

Thanks

Berriel
  • 12,659
  • 4
  • 43
  • 67
mokiliii Lo
  • 587
  • 3
  • 13
  • 26
  • 1
    simply `c=a+b`? This will element-wise add arrays `a` and `b` into a new array `c`. – Bart Feb 29 '20 at 11:25
  • seems like you have a list. import numpy as np ; np.array(a) + np.array(b) ? – StupidWolf Feb 29 '20 at 11:36
  • thanks, yes it does answer my question but do you have any idea how to deal with arrays that have the shape (9,9) for example? I would like to know how to "convert" any array that isn't (10,10) into a (10,10), by just adding zeroes. – mokiliii Lo Feb 29 '20 at 11:43

1 Answers1

1

Try the .add method for a numpy array:

sum = np.add(firstarray, secondarray)
David Collins
  • 848
  • 3
  • 10
  • 28
  • thanks, any idea how to deal with arrays that have the shape (9,9) for example? I would like to know how to "convert" any array that isn't (10,10) into a (10,10), by just adding zeroes. – mokiliii Lo Feb 29 '20 at 11:34
  • Check this out: https://docs.scipy.org/doc/numpy/reference/generated/numpy.pad.html, or ask in another question for more details. There's really not the space in comments. – David Collins Feb 29 '20 at 20:47