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

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

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)


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

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?

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

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.

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.

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)

1 Like

Thank you. This works now… Appreciated please

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

coverage[i,j] = 1

with

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:

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

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

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