views:

79

answers:

1

How can I quickly extract two rows of a scipy.sparse.lil_matrix and apply bitwise operations on them? I've tried:

np.bitwise_and(A[1,:], A[2,:])

but NumPy seems to want an array type according to the documentation.

+1  A: 

By "lil_matrix", do you mean a scipy.sparse.lil_matrix? If so, you'll have to convert your sparse array to a normal dense array to do bitwise operations on it, I believe.

a = np.asarray(A.todense())
np.bitwise_and(a[1,:], a[2,:])

Should do the trick, I think...

EDIT: Forgot an "asarray" there...

Joe Kington
If the matrix has a lot of rows, you might want to do np.asarray(A[1,:].todense()) for each row.
Justin Peel
With Justin's suggestion, your method works great :)
Ryan Rosario