apply a function on two ndarrays's corresponding rows at once(without for loop)
<p>I would like to apply one function to two ndarray's corresponding elements at once without using a for loop. Let's say I have the following two ndarrays <code>x</code> and <code>y</code> and a function <code>foo</code> that takes in two 1d-arrays and compute the <code>beta</code>.
The end result I want is to compute <code>beta00 = foo(x[0, 0],y[0])</code>, <code>beta01 = foo(x[0, 1], y[1])</code>, <code>beta10 = foo(x[1, 0],y[0])</code>, <code>beta11 = foo(x[1, 1], y[1])</code> and yield a expected result of </p>

<pre><code>   [[beta00, beta01],
    [beta10, beta11]]
</code></pre>

<p>I have been looking into vectorize function and apply function, but still don't have a solution. Could someone help me on this? Many thanks in advance.</p>

<pre><code>import numpy as np
x = np.array([[[0, 1, 2, 3], [0, 1, 2, 3]],
              [[2,3,4,5], [2,3,4,5]]])
y = np.array([[-1, 0.2, 0.9, 2.1], [-1, 0.2, 0.9, 2.1]])

def foo(x,y):
    A = np.vstack([x, np.ones(x.shape)]).T
    return np.linalg.lstsq(A, y)[0][0]
</code></pre>