# Bounds Error?

**URL:** <https://discourse.julialang.org/t/bounds-error/84953>\
**Category:** General Usage\
**Tags:** indexing, arrays\
**Created:** [July 29, 2022, 1:26am UTC](https://discourse.julialang.org/t/bounds-error/84953 "2022-07-29T01:26:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ejlongman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ejlongman/32/36801_2.png) [@ejlongman](https://discourse.julialang.org/u/ejlongman)\
**Post date:** [July 29, 2022, 1:26am UTC](https://discourse.julialang.org/t/bounds-error/84953/1 "2022-07-29T01:26:16Z")

</div>

I am trying to run the following code:

```
u_vect = rand(5,2)   
for i in eachindex(u_vect)
    println(i)
    a = u_vect[i,2] 
end

```

And get the error: “BoundsError: attempt to access 5×2 Matrix{Float64} at index [6, 2].” Why does my 5x2 vector have 6 index positions? I know that arrays are considered one dimensional, but that still doesn’t explain the error?

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [July 29, 2022, 2:35am UTC](https://discourse.julialang.org/t/bounds-error/84953/2 "2022-07-29T02:35:31Z")

</div>

`eachindex` goes over every index of the matrix, since your matrix has 10 entries in total, it goes from 1 to 10:

```julia
julia> eachindex(u_vect)
Base.OneTo(10)

```

If you only want to iterate over the first dimension of the matrix, you’ll need to use the `axes` function, with the desired dimension:

```julia
julia> axes(u_vect,1)
Base.OneTo(5)

julia> axes(u_vect,2)
Base.OneTo(2)

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 29, 2022, 2:36am UTC](https://discourse.julialang.org/t/bounds-error/84953/3 "2022-07-29T02:36:42Z")

</div>

`eachindex(u_vect)` gives you the linear indices of every element in the matrix:

```julia
julia> eachindex(u_vect)
Base.OneTo(10)

```

So by doing `u_vect[i, 2]`, you’re trying to access `u_vect[1, 2], u_vect[2, 2], ..., u_vect[10, 2]`, which doesn’t work (it errors as soon as you get to `u_vect[6, 2]`).

If you’re using `eachindex`, then you want to index with _just_ i, as in:

```julia
for i in eachindex(u_vect)
  a = u_vect[i]
  ...
end

```

or if you just want to iterate over the indices in the _first_ dimension, then you want `axes`:

```julia
for i in axes(u_vect, 1)
  a = u_vect[i, 2]
  ...
end

```

---

<div class="post-metadata">

**Author:** ![ejlongman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ejlongman/32/36801_2.png) [@ejlongman](https://discourse.julialang.org/u/ejlongman)\
**Post date:** [July 29, 2022, 2:41pm UTC](https://discourse.julialang.org/t/bounds-error/84953/4 "2022-07-29T14:41:19Z")

</div>

> [@jebej](#):
>
> If you only want to iterate over the first dimension of the matrix, you’ll need to use the `axes` function, with the desired dimension:

thank you so much!

---

<div class="post-metadata">

**Author:** ![ejlongman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ejlongman/32/36801_2.png) [@ejlongman](https://discourse.julialang.org/u/ejlongman)\
**Post date:** [July 29, 2022, 2:41pm UTC](https://discourse.julialang.org/t/bounds-error/84953/5 "2022-07-29T14:41:38Z")

</div>

thank you!!!
