Given a row vector a = np.array([1, 2, 3]) and a column vector b = np.array([[1], [2], [3]]) we can compare all elements one by one by executing c = a==b which returns
>>> c
array([[ True, False, False],
[False, True, False],
[False, False, True]])
However, when the number of elements is very large this demands a lot of memory. Is it possible use the sparse matrices a and b below and compute a sparse c matrix efficiently?
from scipy.sparse import csr_matrix
data = np.array([1, 2, 3])
row = np.array([0, 1, 2])
col = np.array([0, 0, 0])
a = csr_matrix((data, (row, col)), shape=(3, 1))
b = csr_matrix((data, (col, row)), shape=(1, 3))