0

I have a row vector R and a column vector C. I want to add them to create an array A with height equal to size of R and width equal to size of C as follows: A[i,j] = R[i] + C[j]

What's the most efficient way of doing this?

Jakub Bartczuk
  • 2,317
  • 1
  • 20
  • 27

1 Answers1

2
R + C[:, numpy.newaxis]

Does the trick for me.

For example

import numpy as np
r = np.ones(5)
c = np.ones(4) * 2
r + c[:, np.newaxis]

gives

array([[ 3.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  3.],
       [ 3.,  3.,  3.,  3.,  3.]])
Jakub Bartczuk
  • 2,317
  • 1
  • 20
  • 27