title: Numpy: find row-wise common element efficiently
body: <p>Suppose we are given two 2D numpy arrays <code>a</code> and <code>b</code> with the same number of rows. Assume furthermore that we know that each row <code>i</code> of <code>a</code> and <code>b</code> has at most one element in common, though this element may occur multiple times. How can we find this element as efficiently as possible?</p>
  
  <p>An example:</p>
  
  <pre><code>import numpy as np
  
  a = np.array([[1, 2, 3],
                [2, 5, 2],
                [5, 4, 4],
                [2, 1, 3]])
  
  b = np.array([[4, 5],
                [3, 2],
                [1, 5],
                [0, 5]])
  
  desiredResult = np.array([[np.nan],
                            [2],
                            [5],
                            [np.nan]])
  </code></pre>
  
  <p>It is easy to come up with a streightforward implementation by applying <code>intersect1d</code> along the first axis:</p>
  
  <pre><code>from intertools import starmap
  
  desiredResult = np.array(list(starmap(np.intersect1d, zip(a, b))))
  </code></pre>
  
  <p>Apperently, using python's builtin set operations is even quicker. Converting the result to the desired form is easy. </p>
  
  <p>However, I need an implementation as efficient as possible. Hence, I do not like the <code>starmap</code>, as I suppose that it requires a python call for every row. I would like a purely vectorized option, and would be happy, if this even exploitet our additional knowledge that there is at most one common value per row. </p>
  
  <p><strong>Does anyone have ideas how I could speed up the task and implement the solution more elegantly?</strong> I would be okay with using C code or cython, but coding effort should be not too much. </p>