title: Row-wise comparison in numpy
body: <p>Say we have two numpy arrays:</p>
  
  <pre><code>arr = np.array([
      [
          [1, 2, 3],
          [4, 5, 6],
          [7, -8, 9],
          [10, 11, 12]
      ],
      [
          [13, 14, -15],
          [16, -17, 18],
          [19, 20, 21],
          [22, 23, 24]
      ]
  ])
  </code></pre>
  
  <p>and</p>
  
  <pre><code>comp = np.array([
      [2, 20, 4],
      [3, 8, 15]
  ])
  </code></pre>
  
  <p>I am trying to compare each 1D array of <code>comp</code> to 2D array of <code>arr</code> (row-wise), i.e. </p>
  
  <pre><code>[2, 20, 4] &gt; [1, 2, 3] = [1 1 1]
  </code></pre>
  
  <p>if the next row doesn't satisfy the condition negate the <code>comp</code> and then compare it:</p>
  
  <pre><code>[-2, 20, -4], [4, 5, 6] = [-1 1 -1]
  </code></pre>
  
  <p>if nothing else satisfies put <code>0</code></p>
  
  <p>And for the second sample from <code>arr</code>, it should compare with the second 1D of <code>comp</code>, i.e:</p>
  
  <pre><code>[2, 20, 4], [13, 14, -15] = [...]
  </code></pre>
  
  <p>So, it should something like, </p>
  
  <pre><code>[
    [
      [1 1 1]
      [-1 1 -1]
      ...
   ]
   [
     [...]
     [...]
   ]
  ]
  </code></pre>
  
  <p>I have tried doing something like this:</p>
  
  <pre><code>for sample in arr:
      for row in sample:
          print(np.where(np.greater(row, comp), 1, np.where(np.less(row, -comp), -1, 0)))
  </code></pre>
  
  <p>But this code is comparing the complete array of <code>comp</code> to <code>arr[0][#]</code> and <code>a[1][#]</code> (alternatively).</p>
  
  <p>How should I do this row wise? </p>
  
  <p><strong>Update:</strong></p>
  
  <p>Is this the right way of doing it?:</p>
  
  <pre><code>for idx, sample in enumerate(arr):
      print(np.where(np.greater(sample, comp[idx]), 1, np.where(np.less(sample, -comp[idx]), -1, 0)))
  </code></pre>