Hi,
I am trying to generate random vector with {-1, +1}
on length n
.
The following code works
y = 2 .* (rand(n) .> 0.50) .- 1
When I try to use @.
, it throws error
y = @. 2 * (rand(n) > 0.50) - 1
ERROR: MethodError: no method matching isless(::Float64, ::Vector{Float64})
Closest candidates are:
isless(::T, ::T) where T<:Union{Float16, Float32, Float64} at float.jl:424
isless(::AbstractVector, ::AbstractVector) at abstractarray.jl:2612
isless(::AbstractFloat, ::AbstractFloat) at operators.jl:184
...
Stacktrace:
[1] <(x::Float64, y::Vector{Float64})
@ Base ./operators.jl:356
[2] >(x::Vector{Float64}, y::Float64)
@ Base ./operators.jl:382
[3] _broadcast_getindex_evalf
@ ./broadcast.jl:670 [inlined]
[4] _broadcast_getindex
@ ./broadcast.jl:643 [inlined]
[5] _getindex
@ ./broadcast.jl:667 [inlined]
[6] _getindex
@ ./broadcast.jl:666 [inlined]
[7] _broadcast_getindex
@ ./broadcast.jl:642 [inlined]
[8] _getindex
@ ./broadcast.jl:666 [inlined]
[9] _broadcast_getindex
@ ./broadcast.jl:642 [inlined]
[10] getindex
@ ./broadcast.jl:597 [inlined]
[11] copy
@ ./broadcast.jl:875 [inlined]
[12] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{0}, Nothing, typeof(-), Tuple{Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{0}, Nothing, typeof(*), Tuple{Int64, Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{0}, Nothing, typeof(>), Tuple{Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{0}, Nothing, typeof(rand), Tuple{Int64}}, Float64}}}}, Int64}})
@ Base.Broadcast ./broadcast.jl:860
[13] top-level scope
@ REPL[51]:1
But, if I create the random vector first and then use @.
, it works again
u = rand(n)
y = @. 2 * (u > 0.50) - 1
I could like to know why I cannot use rand()
with @.
?