# ERROR: LoadError: indexed assignment not defined for FloatRange{Float64}

**URL:** <https://discourse.julialang.org/t/error-loaderror-indexed-assignment-not-defined-for-floatrange-float64/1668>\
**Category:** General Usage\
**Created:** [January 24, 2017, 8:38pm UTC](https://discourse.julialang.org/t/error-loaderror-indexed-assignment-not-defined-for-floatrange-float64/1668 "2017-01-24T20:38:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![phillefjonk](https://avatars.discourse-cdn.com/v4/letter/p/f04885/32.png) [@phillefjonk](https://discourse.julialang.org/u/phillefjonk)\
**Post date:** [January 24, 2017, 8:38pm UTC](https://discourse.julialang.org/t/error-loaderror-indexed-assignment-not-defined-for-floatrange-float64/1668/1 "2017-01-24T20:38:51Z")

</div>

Why does this not work:

```julia
xVector=0:0.01:2*pi;
yVector=0.0*xVector;
for n=1:length(xVector)
yVector[n] = sin(xVector[n])
end
plot(xVector, yVector)

```

What is the correct way to do this?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [January 24, 2017, 8:52pm UTC](https://discourse.julialang.org/t/error-loaderror-indexed-assignment-not-defined-for-floatrange-float64/1668/2 "2017-01-24T20:52:12Z")

</div>

`yVector = similar(xVector)`

---

<div class="post-metadata">

**Author:** ![PabloZubieta](https://avatars.discourse-cdn.com/v4/letter/p/ee7513/32.png) [@PabloZubieta](https://discourse.julialang.org/u/PabloZubieta)\
**Post date:** [January 24, 2017, 8:52pm UTC](https://discourse.julialang.org/t/error-loaderror-indexed-assignment-not-defined-for-floatrange-float64/1668/3 "2017-01-24T20:52:28Z")

</div>

A couple of options

```julia
X = 0:0.01:2pi
Y = similar(X);
for i in eachindex(X) # or 1:length(X)
    Y[i] = sin(X[i])
end
plot(X, Y)

```

or

```julia
X = 0:0.01:2pi
Y = similar(X);
Y .= sin.(X)

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [January 24, 2017, 8:53pm UTC](https://discourse.julialang.org/t/error-loaderror-indexed-assignment-not-defined-for-floatrange-float64/1668/4 "2017-01-24T20:53:47Z")

</div>

> [@phillefjonk](#):
>
> xVector=0:0.01:2_pi;  
> yVector=0.0_xVector;  
> for n=1:length(xVector)  
> yVector[n] = sin(xVector[n])  
> end

Or to avoid writing the loop explicitly

`yVector = sin.(xVector)`

---

<div class="post-metadata">

**Author:** ![phillefjonk](https://avatars.discourse-cdn.com/v4/letter/p/f04885/32.png) [@phillefjonk](https://discourse.julialang.org/u/phillefjonk)\
**Post date:** [January 24, 2017, 9:24pm UTC](https://discourse.julialang.org/t/error-loaderror-indexed-assignment-not-defined-for-floatrange-float64/1668/5 "2017-01-24T21:24:21Z")

</div>

\*\*\*\*\*\* Thank you! \*\*\*\*\*\*

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [January 26, 2017, 6:09am UTC](https://discourse.julialang.org/t/error-loaderror-indexed-assignment-not-defined-for-floatrange-float64/1668/6 "2017-01-26T06:09:30Z")

</div>

> [@phillefjonk](#):
>
> Why does this not work:

`a:b:c` does not create a vector. It creates a `Range`, which is a type that looks and indexes like a vector, but actually doesn’t allocate the memory for the vector. So indexing it is essentially calling a function, and it computes what the value should be, instead of storing it. When you get that, then you see why you cannot “set” values for it.

But, for these kinds of “lazy collections” (of which `LinSpace` is another), you can always `collect` them to a `Vector` via `collect(a:b:c)`. Then you have a proper array which can set values, but now it takes up more memory (and is thus slower in practice, so don’t just collect everything like MATLAB!)

Hope that makes sense.

---

<div class="post-metadata">

**Author:** ![phillefjonk](https://avatars.discourse-cdn.com/v4/letter/p/f04885/32.png) [@phillefjonk](https://discourse.julialang.org/u/phillefjonk)\
**Post date:** [January 26, 2017, 7:57am UTC](https://discourse.julialang.org/t/error-loaderror-indexed-assignment-not-defined-for-floatrange-float64/1668/7 "2017-01-26T07:57:13Z")

</div>

Very good explanation. Now it is crystal clear, thank you!

---

<div class="post-metadata">

**Author:** ![felix](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/felix/32/604_2.png) [@felix](https://discourse.julialang.org/u/felix)\
**Post date:** [January 26, 2017, 8:03am UTC](https://discourse.julialang.org/t/error-loaderror-indexed-assignment-not-defined-for-floatrange-float64/1668/8 "2017-01-26T08:03:37Z")

</div>

Maybe the error message could be improved? Currently

```
julia> xx = 0:0.1:2
0.0:0.1:2.0

julia> xx[3] = 5
ERROR: indexing not defined for FloatRange{Float64}
Stacktrace:
 [1] setindex!(::FloatRange{Float64}, ::Int64, ::Int64) at .\abstractarray.jl:873

julia> xx[3]
0.2

```

It’s confusing because “indexing” looks like it is defined – as in, indexed-get works. And the `eltype` is irrelevant to the problem.  
Would an error message like the following be a good idea?

```
julia> xx[3] = 5
ERROR: indexed-set not defined for FloatRange, try making a copy with `collect` or `similar`

```

---

<div class="post-metadata">

**Author:** ![Arghavan\_Partovifar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arghavan_partovifar/32/18262_2.png) [@Arghavan\_Partovifar](https://discourse.julialang.org/u/Arghavan_Partovifar)\
**Post date:** [September 28, 2020, 4:14pm UTC](https://discourse.julialang.org/t/error-loaderror-indexed-assignment-not-defined-for-floatrange-float64/1668/9 "2020-09-28T16:14:35Z")

</div>

Thank you very much
