# I keep getting this error--- syntax: invalid iteration specification

I am trying to run this code in code in Julia but I keep getting this error which am struggling to correct. syntax: invalid iteration specification

Any help will be much appreciated please. Below is my code:

x = [0, 5, -5, 4, -5] # x cordinates of the circle
y = [0, 0.6, 0.5, -5, -4.5] # y cordinates of the circle

coverage=[[1, 0, 0, 0, 1],[0, 0, 0, 1, 1],[1, 0, 0, 0, 1],[0, 0, 1, 1, 1],[0, 0, 0, 1, 1], [0, 1, 0, 0, 1],[0, 0, 1, 1, 0],[0, 0, 0, 1, 0],[1, 0, 0, 0, 0],[1, 1, 0, 0, 0],[0, 1, 0, 0, 0],[0, 0, 1, 0, 0],[0, 0, 1, 0, 0],[1, 0, 0, 0, 0],[0, 0, 0, 1, 0]]
#A=[[2, 3, 4], [3, 2, 5]]
a=[-1, -1, 1.2, 1.2, 1.2, 0.8, 2.5, 2.5, -3, -3, -1.5, 3, 0.9, -3.7, 3.7] #x cordinate of the points
b=[0.2, 0.8, -0.85, 1.2, 0.5, -1.5, -1.3, 1.4, 1.5, -1.3, -0.8, -2.7, -2.4, 0.5, 0.8] #y cordinate of the points
r=[]        # array to store max distances for the center of each circle to each point
#print("A =", A)
print("A =", coverage)

function  distance()
for i,xv in enumerate(x):
R=[] # store the distance of each circle i to each point
for j, jv in enumerate(a):
if (coverage[j][i]==1):
R.append( ((x[i]-a[j])**2 + (y[i]-b[j])**2)**0.5)
r.append(max(R)) # find maximum distance from circle i to each point and save in r
# end
return r

distance()

Hi, this looks like a badly formatted incomplete translation from Python to Julia. Would you be so kind as to use triple back ticks to mark down your code?

for (i, xv) in enumerate(x)

should help.

4 Likes

itβs not valid Julia code
it should be something like this

function distance(x, y, a, b)
r = Float64[]
for (i,xv) in enumerate(x)
R = Float64[]
for (j, jv) in enumerate(a)
if coverage[j][i] == 1
push!(R, ((x[i]-a[j])^2 + (y[i]-b[j])^2)^0.5)
end
if length(R) > 0
push!(r, maximum(R))
end
end
end
return r
end

Although that is still not optimal because of the allocation of R and pushing to r inside the loop.

I believe you should calculate the values for R and extract the r separately.

1 Like

Thank you very much. I tried that but had this displaying now instead of the output I desire. It reads:

distance (generic function with 2 methods)

thank you

you have to call the function

distance(x, y, a, b)

which will display the returned r

1 Like

Thank you

1 Like

I accidentally missed out passing coverage as an argument

But I hope you can get to grips with it all

I was trying to figure that out.
Where should I put that in my code?

I am very new to Julia. The syntax of python sometimes confuses me with Julia. I hope to improve gradually in the near future. Thank you very much for your help.

2 Likes

just as another argument

function distance(x, y, a, b, coverage)
1 Like

Thank you very much. This works perfectly now but giving me all the distances calculated for R. I want to take the maximum value of each of this distances stored in βRβ and save it βrβ. I am expecting only 5 outputs in all. Is there any way I can achieve this?

Seems we ran into the problem of using significant whitespace so appending to r ended up inside the inner loop.

Here I have moved it outside.

function distance(x, y, a, b, coverage)
r = Float64[]
for (i,xv) in enumerate(x)
R = Float64[]
for (j, jv) in enumerate(a)
if coverage[j][i] == 1
push!(R, ((x[i]-a[j])^2 + (y[i]-b[j])^2)^0.5)
end
end
if length(R) > 0
push!(r, maximum(R))
end
end
return r
end

You mean as a visual indicator, right? The fault was a misplaced end, the indentation is luckily irrelevant here

This works perfectly now. Thank you very much for the help.
Highly appreciated

1 Like