# Error while indexing an array

**URL:** <https://discourse.julialang.org/t/error-while-indexing-an-array/105274>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [October 22, 2023, 4:29am UTC](https://discourse.julialang.org/t/error-while-indexing-an-array/105274 "2023-10-22T04:29:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jddorellanao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jddorellanao/32/44261_2.png) [@jddorellanao](https://discourse.julialang.org/u/jddorellanao)\
**Post date:** [October 22, 2023, 4:29am UTC](https://discourse.julialang.org/t/error-while-indexing-an-array/105274/1 "2023-10-22T04:29:47Z")

</div>

I’m trying to code the next equation:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/9/79b4a3338687c9bc57118e1e635bae3eb5fca121.png)

Here,

1. `S` is an array of size `(n,n)`
2. `A` is an array of size `n`
3. `H` is an array of size `(n,n)`
4. `T` is an array of size `n`
5. `Ť` is an array of size `n` with the cumulative sum of `T` (`Tc` in the code)

So far my code is:

```julia
for i in 1:n
    for i_ in 1:n
        if H[i, i_] <= Tc[1]
            S[i, i_] = A[1] * cos(2π * H[i, i_] / T[1]) - A[1]

        elseif Tc[i_-1] < H[i, i_] <= Tc[i_]
            S[i, i_] = A[i_] * cos(2π * H[i, i_] / T[i_]) - A[i_]
            
        elseif H[i, i_] > Tc[i_-1]
            S[i, i_] = A[n] * cos(2π * H[i, i_] / T[n]) - A[n]
        end
    end
end

```

The code gives the next error:

`BoundsError: attempt to access n-element Vector{Float64} at index [0]`

I tried it in Python but I guess it worked there because you can index with negative integers, which leads me to think that the problem is my approach.

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [October 22, 2023, 5:51am UTC](https://discourse.julialang.org/t/error-while-indexing-an-array/105274/2 "2023-10-22T05:51:55Z")

</div>

> [@jddorellanao](#):
>
> ```julia
> elseif H[i, i_] > Tc[i_-1]
> 
> ```

This should be `Tc[n-1]` according to your definition.

Also, this throws an error (due to `Tc[i-1]`) whn i=1:

> [@jddorellanao](#):
>
> ```julia
> elseif Tc[i_-1] < H[i, i_] <= Tc[i_]
> 
> ```

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [October 22, 2023, 6:04am UTC](https://discourse.julialang.org/t/error-while-indexing-an-array/105274/3 "2023-10-22T06:04:11Z")

</div>

Stylistic considerations unrelated to the error:

1. Avoid code duplication by putting the assignment outside the `if`, like `S[i, i_] = ifH[i, i_] <= Tc[1]`
2. Use `cospi(2*x)` instead of `cos(2π)`, especially if the element type is not necessarily `Float64`.
3. Maybe assign `H[i, i_]` to a variable, and use it to avoid code duplication
4. Maybe do `for i in 1:n, j in 1:n` instead of the two nested `for`s

---

<div class="post-metadata">

**Author:** ![jddorellanao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jddorellanao/32/44261_2.png) [@jddorellanao](https://discourse.julialang.org/u/jddorellanao)\
**Post date:** [October 22, 2023, 9:48am UTC](https://discourse.julialang.org/t/error-while-indexing-an-array/105274/4 "2023-10-22T09:48:08Z")

</div>

Yes, you are right, it should be `Tc[n-1]`.

And yes, that’s why I asked the question, because each value of `H` has to be evaluated in an interval of `Tc[i-1]` - `Tc[i]`. But when `i=1` the index will be `0` resulting in an error.

---

<div class="post-metadata">

**Author:** ![jddorellanao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jddorellanao/32/44261_2.png) [@jddorellanao](https://discourse.julialang.org/u/jddorellanao)\
**Post date:** [October 22, 2023, 9:51am UTC](https://discourse.julialang.org/t/error-while-indexing-an-array/105274/5 "2023-10-22T09:51:04Z")

</div>

Thank you very much! I appreciate the stylistic suggestions, I have already implemented them 😄

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [October 22, 2023, 10:05am UTC](https://discourse.julialang.org/t/error-while-indexing-an-array/105274/6 "2023-10-22T10:05:43Z")

</div>

> [@jddorellanao](#):
>
> `Ť` is an array of size `n` with the cumulative sum of `T` (`Tc` in the code)

Note that Julia identifiers are Unicode strings, so you could just use `Ť` instead of `Tc`, if you think that suits you better.

> [@jddorellanao](#):
>
> But when `i=1` the index will be `0` resulting in an error.

As far as I understand, your problem is that you want zero-based indexing, instead of the one-based indexing that’s used by default? While Julia’s `Array`s (including `Matrix` and `Vector`) use one-based indexing, there’s a few approaches to changing the indexing base:

While the `i` in `arr[i]` is one-based when `arr isa Array`, the `i` in `arr[begin + i]` effectively shifts the indexing base to zero, because the `begin` [evaluates](https://docs.julialang.org/en/v1/base/base/#begin) `firstindex(arr)` (when used in an indexing expression). If writing `begin + ...` many times is too unwieldy, just create a helper function. For example:

```julia
get = (v, i) -> v[begin + i]
set! = (v, i, n) -> v[begin + i] = n
a = [10, 20]
set!(a, 0, get(a, 1))

```

It’s even possible to overload binary operators for this purpose, if you fancy that.

Alternatively, you could use another `AbstractArray` type, such as those provided by the `OffsetArrays` package, enabling you to explicitly choose an indexing base when constructing the array.

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [October 22, 2023, 5:20pm UTC](https://discourse.julialang.org/t/error-while-indexing-an-array/105274/7 "2023-10-22T17:20:51Z")

</div>

I do not think OP is interested in zero-based indexing.

> [@jddorellanao](#):
>
> And yes, that’s why I asked the question, because each value of `H` has to be evaluated in an interval of `Tc[i-1]` - `Tc[i]`. But when `i=1` the index will be `0` resulting in an error.

This is not a coding question though. Based on the definition you provided in the first post, \hat{T}\_i seems to exist for i=1, \ldots, n. Hence, the (second) condition \hat{T}\_{i-1} \< H \leq \hat{T}\_i needs to be adjusted for i=1 because there is no \hat{T}\_0. So you need to decide how the condition reads for i=1.

For example, one option would be to only check H \leq \hat{T}\_i when i=1. We can turn that easily into code:

```julia
elseif (i_ == 1 || Tc[i_-1] < H[i, i_]) && (H[i, i_] <= Tc[i_])

```
