I want to convert a number(eg: 3
) to it's logical array( [0 0 1 0 0 0 0 0 0 0]
). In matlab, we can use
a = 1:10
b = 3
a == b
then, we can get 0 0 1 0 0 0 0 0 0 0
. How can I get it in python, because when I try this in python, I got:
In [220]: import numpy as np
In [221]: a = np.arange(10)
In [222]: b = 3
In [223]: a == b
Out[223]: array([False, False, False, True, False, False, False, False, False, False], dtype=bool)
之后可以将其转换为整数:
(a == b).astype(int)
Actually, if you ant the same output as MATLAB
, you need
np.asarray(a + 1 == b).astype(np.int32)
or to define a
as
a = np.arange(1,11)
您可以将n a == b
替换a == b
np.array(a == b).astype(np.int32)
通过更改类型将解决您的问题。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.