Issue
I have 2 vectors, I would like to compute a matrix where the i'th row is the first vector compared to the i'th element of the second vector. This code does that:
a1 = np.array([1, 2, 3])
a2 = np.array([1, 2, 3])
print(np.array([a1 > e for e in a2]))
where we get:
[[False True True]
[False False True]
[False False False]]
I want to have the same behavior but done efficiently with "numpy magic". How can I do this?
Solution
You can broadcast the function by making one of the arrays have one singleton dimension:
In [1]: import numpy as np
In [2]: a1 = np.array([1, 2, 3])
...: a2 = np.array([1, 2, 3])[:,None]
In [3]: a1>a2
Out[3]:
array([[False, True, True],
[False, False, True],
[False, False, False]])
Answered By - jhso
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.