 <p>Unless you want to dig into the internals of one or more sparse matrix types, you should use CSR format for your matrix and:</p> <ul> <li>Calculate the length (L2 norm) of each matrix row; in other words: <code>sum(multiply(M, M), 2)</code></li> <li>Normalize r to (L2) length 1</li> <li>Matrix multiply <code>M*r</code> (where r is treated as a column vector)</li> </ul> <p>If an entry of <code>M*r</code> matches the length of the corresponding row, then you have a match.</p> <p>Note that the default <code>ord</code> for <code>numpy.linalg.norm</code> is L2 norm.</p>