What is the most efficient way to compare every value of 2 numpy matrices?
<p>I'd like to more efficiently take every value of 2 matrices(<code>a</code> and <code>b</code>) of the same size and return a third boolean(or 1/ 0 matrix to make things clean) into matrix<code>c</code> containing the results of the conditions.</p>

<p>Example:</p>

<p>Condition: <code>For a == 0 and b == 3</code></p>

<pre><code>a = [[1 0]
    [0 1]] 

b = [[3 5]
    [3 9]] 
</code></pre>

<p>Would return:</p>

<pre><code>c = [[0 0]
    [1 0]]
</code></pre>

<p><code>[0,1]</code> is the only place where <code>a == 0</code> and <code>b == 3</code> so it is the only place <code>True</code> in c </p>

<p>This is the code I have so far:</p>

<pre><code>import numpy as np

a = np.matrix("1, 0; 0 1")
print(a,'\n')
b = np.matrix("3, 5; 3 9")
print(b,'\n')

c = []
for x in range(0,np.shape(a)[1]):
    row = []
    for y in range(0,np.shape(a)[1]):
        row.append(int(a[x,y] == 0 and b[x,y] == 3)) # the int() is there just to keep things tighty for the 3 prints 
    c.append(row)
c = np.matrix(c)
print(c)
</code></pre>

<p>results:</p>

<pre><code>[[1 0]
 [0 1]] 

[[3 5]
 [3 9]] 

[[0 0]
 [1 0]]
</code></pre>

<p>I could also use:</p>

<pre><code>a=a==0
b=b==3
c=a&amp;b
</code></pre>

<p>But that would require making a copy of a and b and with big matrices would that still be efficient ?</p>

<p>Why can't I just use <code>a == 0 &amp; b == 3</code> ?</p>

<p>I need to do a comparison like this for several matrices that are 1000+ size so you could see where iterating thought them would be quite slow.</p>

<p>Thank you very much for any help I'm sure the answer is something simple and right in front of me but I'm just dumb.</p>

<p>You <em>can</em> use (pretty) much the expression that you wanted:</p>

<pre><code>&gt;&gt;&gt; (a == 0) &amp; (b == 3)
matrix([[False, False],
        [ True, False]], dtype=bool)
</code></pre>

<p>Beware, you <em>need</em> the parenthesis to make the precendence work out as you'd like -- Normally <code>&amp;</code> will bind tighter than <code>==</code>.  If you don't like the extra parenthesis, you can use the more verbose (though arguably more semantically correct) <code>np.logical_and</code> function.</p>

<p>Also note that while no <em>copies</em> are being made, there are <em>temporary</em> arrays being created.  Specifically, the result of <code>a == 0</code> and <code>b == 3</code> are both going to be allocated and freed in this statement.  Generally, that's not such a big deal and numpy's vectorized operations remain fast.  However, if that <em>isn't</em> fast enough for you, you <em>can</em> use a library like <a href="https://github.com/pydata/numexpr" rel="nofollow"><code>numexpr</code></a> to remove the temporary arrays:</p>

<pre><code>&gt;&gt;&gt; numexpr.evaluate('(a == 0) &amp; (b == 3)')
array([[False, False],
       [ True, False]], dtype=bool)
</code></pre>

<p>And of course, if you need <code>1</code> and <code>0</code>, you can use <code>result.astype(int)</code> on the output array to make arrays of ints rather than booleans.</p>

python python-3.x oop numpy matrix