【发布时间】:2015-02-05 21:57:53
【问题描述】:
这是我目前正在做的事情:
a = trues(100)
for i in 1:length(a)
a[i] = rand()>0.5 ? true : false
end
有更好(更快)的解决方案吗?
【问题讨论】:
这是我目前正在做的事情:
a = trues(100)
for i in 1:length(a)
a[i] = rand()>0.5 ? true : false
end
有更好(更快)的解决方案吗?
【问题讨论】:
在 Julia 0.4 中你可以写 bitrand(100):
julia> bitrand(100)
100-element BitArray{1}:
true
true
false
false
true
⋮
true
false
true
true
true
您可以使用旧版本 Julia 中的 Compat 包来获取此功能,或者您可以使用旧名称 randbool(相同的行为,不同的名称)。西蒙对rand(Bool,100) 的回答有效,但它给出了Array{Bool} 而不是BitArray——一种特殊的数据类型,它紧凑地存储布尔数组,每个布尔值只使用一个位。
【讨论】:
Pkg.update() 然后using compat.jl,我收到Warning: requiring "compat" did not define a corresponding module 和bitrand 没有定义。如果这不能轻易解决,我想我会打开一个新帖子。谢谢
Pkg.add("Compat") 然后using Compat。在 Julia 中使用包时,通常会省略 .jl 后缀。
INFO: No packages to install, update or remove INFO: Package database updated。然后我写了using Compat(没有消息返回),bitrand没有定义。
【讨论】: