I have been writing scripts in Julia recently, and have run across a problem using the setindex! function that I cannot find an answer to in any documentation (I have also searched stackoverflow, but could not find an answer - my apologies if my search was not good enough and I am repeating a question).
I am getting a MethodError relating to set index with code similar to the following (the error also appears in this code, which is altered simply to make it simpler):
a = 0:0.01:1
a = 2 * pi * (a - 0.4)
a[abs(a) .> pi] += - sign(a[a .> pi]) * 2 * pi
I realize that in the above code I could achieve a similar effect by simply changing the initial expression used to generate a so that it is never greater than pi in magnitude, but in the original code this would be much less readable due to intermediate steps that are not included - additionally, regardless of whether that is possible with this particular problems, there will be other instances using setindex! similarly which I would like to have a solution to.
I have tried using integer indexes instead of logical indexes and have tried storing the logical or integer index as another value. Neither has worked. I would guess this is coming from a fairly basic misunderstanding on my part, but thought this would be a good resource for help.
Thanks in advance
You haven't materialized the FloatRange
into an Array
, so there aren't really any indices to play with yet. It's just a rangelike object:
julia> a = 0:0.01:1
0.0:0.01:1.0
julia> a = 2 * pi * (a - 0.4)
-2.5132741228718345:0.06283185307179587:3.769911184307752
julia> dump(a)
FloatRange{Float64}
start: Float64 -251.32741228718345
step: Float64 6.283185307179586
len: Float64 101.0
divisor: Float64 100.0
Compare with:
julia> a = [a]
101-element Array{Float64,1}:
-2.51327
-2.45044
-2.38761
[...]
3.64425
3.70708
3.76991
after which
julia> maximum(a)
3.769911184307752
julia> a[abs(a) .> pi] += - sign(a[a .> pi]) * 2 * pi;
julia> maximum(a)
3.141592653589793
It's the difference between
julia> 1:2:9
1:2:9
julia> [1:2:9]
5-element Array{Int32,1}:
1
3
5
7
9
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With