python - How to generate a bool 2D arrays from two 1D arrays using numpy -
i have 2 arrays a=[1,2,3,4]
, b=[2,3]
. wondering there efficient way construct boolean 2d array c
(2d matrix, i.e. 2*4 matrix) based on array element comparsions, i.e. c[0,0] = true
iff a[0] == b[0]
. basic way iterate through elements of a
, b
, think there maybe better using numpy
. checked numpy
reference, not find routine that.
thanks
if understood question correctly, can extend dimensions of b
np.newaxis/none
form 2d
array , perform equality check against a
, bring in broadcasting
vectorized solution, -
b[:,none] ==
sample run -
in [5]: out[5]: array([1, 2, 3, 4]) in [6]: b out[6]: array([2, 3]) in [7]: b[:,none] == out[7]: array([[false, true, false, false], [false, false, true, false]], dtype=bool)
Comments
Post a Comment