I have some MWE below. What I want is to have a subsection of a range, interact with the rest of the range, but not itself.
Also, I posted this on stackoverflow … I should have put it here first I think…
For instance if the range is 1:100
, I want to have a for loop that will have each index in 4:6
, interact with all values of 1:100
BUT NOT 4:6
.
I want to do this using ranges/filters to avoid generating temporary arrays.
In my case the total range is the number of atoms in the system. The sub-range, is the atoms in a specific molecule. I need to do calculations where each atom in a molecule interacts with all other atoms, but not the atoms in the same molecule.
Further
I am trying to avoid using if statements because that can potentially complicate parallelizing the code later (such is my impression, correct me if I am wrong). Doing this with an if statement would be
for i=4:6
for j = 1:100
if j == 4 || j==5 || j==6
continue
end
println(i, " ", j) #roughly 0.1-100 microseconds to run the real call here
end
end
I have actual indexing in my code, I would never hardcode values like the above… But I want to avoid that if
statement.
Trials
The following does what I want, but I now realize that using filter
is bad when it comes to memory and the amount used scales linearly with b
.
a = 4:6
b = 1:100
for i in a
for j in filter((b) -> !(b in a),b)
print(i, " ", j)
end
end
Is there a way to get the double for loop I want where the outer is a sub-range of the inner, but the inner does not include the outer sub-range and most importantly is fast and does not create alot of memory usage like filter does?