# BoundsError: attempt to access 25×10 Matrix{Float64} at index \[2, 21\]

**URL:** <https://discourse.julialang.org/t/boundserror-attempt-to-access-25x10-matrix-float64-at-index-2-21/86869>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [September 7, 2022, 1:47am UTC](https://discourse.julialang.org/t/boundserror-attempt-to-access-25x10-matrix-float64-at-index-2-21/86869 "2022-09-07T01:47:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric.O](https://avatars.discourse-cdn.com/v4/letter/e/9dc877/32.png) [@Eric.O](https://discourse.julialang.org/u/Eric.O)\
**Post date:** [September 7, 2022, 1:47am UTC](https://discourse.julialang.org/t/boundserror-attempt-to-access-25x10-matrix-float64-at-index-2-21/86869/1 "2022-09-07T01:47:36Z")

</div>

I have written a code with the aim of getting a 25×10 Matrix at the final output but this doesnt seem to be working for me. I tried correcting this for the past days but I think I have a problem with my for loop. I kindly need help to get this done. I keep getting the error " BoundsError: attempt to access 25×10 Matrix{Float64} at index [2, 21] " but having a challenge correcting this. I need help please. Attached is the code please

```julia
a=[11.5, 13.4, 4.2, 39.9, 14.2, 17.1, 24.6, 30.8, 37.5, 12.7, 49.1, 27.3, 45.6, 3.4, 48.5, 48.5, 8.6, 11.2, 27.1, 29.4, 3.5, 29.1, 38.9, 13.4, 44.7]
b=[43.2, 42.6, 39.9, 38.4, 47.5, 22.0, 14.1, 18.3, 27.3, 26.3, 44.6, 14.1, 11.1, 30.8, 30.6, 19.0, 39.7, 34.9, 1.3, 9.3, 15.8, 6.8, 18.6, 17.2, 9.7]
n=10
m=25
r=10
len=50
using Random
Random.seed!(1235)
using Distributions
xs=rand(Uniform(1,len),n)
ys=rand(Uniform(1,len),n)
print(xs,ys)
function distance(xs, ys, a, b,n,m, r) 
# n = length(xs)
# m = length(a)
    coverage = zeros(m, n) # create the matrix and fill it with zeros
    for i in 1:n
        for j in 1:m
            if sqrt( (xs[i]-a[j])^2 + (ys[i]-b[j])^2 ) <= r
                coverage[i,j] = 1
                @show i j coverage # display the matrix in each iteration
            end
         end
    end
    return coverage
end
coverage=distance(xs, ys, a, b,n,m,r)

```

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [September 7, 2022, 2:34am UTC](https://discourse.julialang.org/t/boundserror-attempt-to-access-25x10-matrix-float64-at-index-2-21/86869/2 "2022-09-07T02:34:51Z")

</div>

Your indexing ranges are the wrong way around.  
Try `i in 1:m, j in 1:n`

---

<div class="post-metadata">

**Author:** ![Eric.O](https://avatars.discourse-cdn.com/v4/letter/e/9dc877/32.png) [@Eric.O](https://discourse.julialang.org/u/Eric.O)\
**Post date:** [September 7, 2022, 2:38am UTC](https://discourse.julialang.org/t/boundserror-attempt-to-access-25x10-matrix-float64-at-index-2-21/86869/3 "2022-09-07T02:38:45Z")

</div>

I still get this error when I try what you suggested please " BoundsError: attempt to access 10-element Vector{Float64} at index [11] "

How do I fix it?

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [September 7, 2022, 2:43am UTC](https://discourse.julialang.org/t/boundserror-attempt-to-access-25x10-matrix-float64-at-index-2-21/86869/4 "2022-09-07T02:43:05Z")

</div>

Match the indexing with vector dimensions.  
eg xs has length n, but now indexing with i in 1:m

---

<div class="post-metadata">

**Author:** ![Eric.O](https://avatars.discourse-cdn.com/v4/letter/e/9dc877/32.png) [@Eric.O](https://discourse.julialang.org/u/Eric.O)\
**Post date:** [September 7, 2022, 2:50am UTC](https://discourse.julialang.org/t/boundserror-attempt-to-access-25x10-matrix-float64-at-index-2-21/86869/5 "2022-09-07T02:50:27Z")

</div>

Can you please make the correction in my code for me. I am trying it but am just confused at this point. Please help me.

---

<div class="post-metadata">

**Author:** ![AndiMD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andimd/32/6531_2.png) [@AndiMD](https://discourse.julialang.org/u/AndiMD)\
**Post date:** [September 7, 2022, 3:31am UTC](https://discourse.julialang.org/t/boundserror-attempt-to-access-25x10-matrix-float64-at-index-2-21/86869/6 "2022-09-07T03:31:11Z")

</div>

In the following, `j` takes values between 1 and 10, and `i` takes values between 1 and 25. So, `i` should index a vector of length 25 (or more), which matches vectors `a` and `b`. `xs` and `ys` have only 10 elements, so you can index them with `j`.

```julia
a=[11.5, 13.4, 4.2, 39.9, 14.2, 17.1, 24.6, 30.8, 37.5, 12.7, 49.1, 27.3, 45.6, 3.4, 48.5, 48.5, 8.6, 11.2, 27.1, 29.4, 3.5, 29.1, 38.9, 13.4, 44.7]
b=[43.2, 42.6, 39.9, 38.4, 47.5, 22.0, 14.1, 18.3, 27.3, 26.3, 44.6, 14.1, 11.1, 30.8, 30.6, 19.0, 39.7, 34.9, 1.3, 9.3, 15.8, 6.8, 18.6, 17.2, 9.7]
n=10
m=25
r=10
len=50
using Random
Random.seed!(1235)
using Distributions
xs=rand(Uniform(1,len),n)
ys=rand(Uniform(1,len),n)
print(xs,ys)
function distance(xs, ys, a, b,n,m, r) 
# n = length(xs)
# m = length(a)
    coverage = zeros(m, n) # create the matrix and fill it with zeros
for j in 1:n
           for i in 1:m
               if sqrt( (xs[j]-a[i])^2 + (ys[j]-b[i])^2 ) <= r
                   coverage[i,j] = 1
                   @show i j coverage # display the matrix in each iteration
               end
            end
       end
    return coverage
end
coverage=distance(xs, ys, a, b,n,m,r)

```

---

<div class="post-metadata">

**Author:** ![Eric.O](https://avatars.discourse-cdn.com/v4/letter/e/9dc877/32.png) [@Eric.O](https://discourse.julialang.org/u/Eric.O)\
**Post date:** [September 7, 2022, 9:41am UTC](https://discourse.julialang.org/t/boundserror-attempt-to-access-25x10-matrix-float64-at-index-2-21/86869/7 "2022-09-07T09:41:57Z")

</div>

> [@AndiMD](#):
>
> ```julia
> a=[11.5, 13.4, 4.2, 39.9, 14.2, 17.1, 24.6, 30.8, 37.5, 12.7, 49.1, 27.3, 45.6, 3.4, 48.5, 48.5, 8.6, 11.2, 27.1, 29.4, 3.5, 29.1, 38.9, 13.4, 44.7]
> b=[43.2, 42.6, 39.9, 38.4, 47.5, 22.0, 14.1, 18.3, 27.3, 26.3, 44.6, 14.1, 11.1, 30.8, 30.6, 19.0, 39.7, 34.9, 1.3, 9.3, 15.8, 6.8, 18.6, 17.2, 9.7]
> n=10
> m=25
> r=10
> len=50
> using Random
> Random.seed!(1235)
> using Distributions
> xs=rand(Uniform(1,len),n)
> ys=rand(Uniform(1,len),n)
> print(xs,ys)
> function distance(xs, ys, a, b,n,m, r) 
> # n = length(xs)
> # m = length(a)
> coverage = zeros(m, n) # create the matrix and fill it with zeros
> for j in 1:n
> for i in 1:m
> if sqrt( (xs[j]-a[i])^2 + (ys[j]-b[i])^2 ) <= r
> coverage[i,j] = 1
> @show i j coverage # display the matrix in each iteration
> end
> end
> end
> return coverage
> end
> coverage=distance(xs, ys, a, b,n,m,r)
> 
> ```

Thank you. This works now… Appreciated please

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 7, 2022, 10:00am UTC](https://discourse.julialang.org/t/boundserror-attempt-to-access-25x10-matrix-float64-at-index-2-21/86869/8 "2022-09-07T10:00:15Z")

</div>

You could have fixed this problem even easier, by simply replacing

```julia
coverage[i,j] = 1

```

with

```julia
coverage[j, i] = 1

```

The error message told you what was wrong

> BoundsError: attempt to access 25×10 Matrix{Float64} at index [2, 21]

namely that the indices were switched.

BTW, you could make this code simpler with broadcasting:

```julia
function distance2(xs, ys, a, b, r)
    coverage = zeros(length(a), length(xs))
    @. coverage = ((a - xs')^2 + (b - ys')^2) <= r^2
    return coverage
end

coverage = distance2(xs, ys, a, b, r)

```

or just

```julia
function distance3(xs, ys, a, b, r)
    return @. Float64((a - xs')^2 + (b - ys')^2 <= r^2)
end

```

---

<div class="post-metadata">

**Author:** ![Eric.O](https://avatars.discourse-cdn.com/v4/letter/e/9dc877/32.png) [@Eric.O](https://discourse.julialang.org/u/Eric.O)\
**Post date:** [September 7, 2022, 10:02am UTC](https://discourse.julialang.org/t/boundserror-attempt-to-access-25x10-matrix-float64-at-index-2-21/86869/9 "2022-09-07T10:02:31Z")

</div>

Thank you. I was fraustrated not getting this work. I am okay now. You saved me. Highly appreciated please
