2

I would like some help with a problem. In Python:

a=array([2,2])
b=ones((2,10))

I would like to know if there is a function that allows me to subtract b-a to have an array of 2x10 full of -1.

I can do it one with 1D arrays, I just wanted to know if it is possible to do with 2D arrays.

Thanks

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
Leon palafox
  • 2,675
  • 6
  • 27
  • 35

2 Answers2

5

Add a new dimension to a:

b - a[:,None]

where a[:,None] becomes array([[2], [2]]), a 2x1 array which you can substract from a 2x10 array and get a 2x10 array full of -1.

eumiro
  • 207,213
  • 34
  • 299
  • 261
  • A bit late to this, but instead of `None` you can use [`numpy.newaxis`](http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#numpy.newaxis). See also http://stackoverflow.com/questions/944863/numpy-should-i-use-newaxis-or-none – Chris Mar 08 '12 at 13:52
0

You want to have an array of 2x10 full of -1.

Why don't you just do like this:

b = np.ones((2, 10)) * -1

array([[-1., -1., -1., -1., -1., -1., -1., -1., -1., -1.],
       [-1., -1., -1., -1., -1., -1., -1., -1., -1., -1.]])
riza
  • 16,274
  • 7
  • 29
  • 29