# Nested loops

**URL:** <https://discourse.julialang.org/t/nested-loops/49810>\
**Category:** General Usage\
**Created:** [November 8, 2020, 11:35pm UTC](https://discourse.julialang.org/t/nested-loops/49810 "2020-11-08T23:35:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![caetanotc](https://avatars.discourse-cdn.com/v4/letter/c/ce73a5/32.png) [@caetanotc](https://discourse.julialang.org/u/caetanotc)\
**Post date:** [November 8, 2020, 11:35pm UTC](https://discourse.julialang.org/t/nested-loops/49810/1 "2020-11-08T23:35:32Z")

</div>

Hi,

I need a program where I’m computing a 2D NxN lattice, and for that I need N^2 nested loops, for N=2 would be:

```julia
for S[1,1] in [-1,1], S[1,2] in [-1,1], S[2,1] in [-1,1], S[2,2] in [-1,1]
    ...
end

```

But I need to compute N in the range 2:5, and doing like that I would need 1 code for each N. Is there a way to optimize that, so I can use just one program?

I tried something like that:

```julia
for i in 1:N, j in 1:N, S[i,j] in [-1,1]
    ...
end

```

but didn’t work as I wanted.

Thanks for the help.

Edit: It was N^2 not N^N

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [November 8, 2020, 11:45pm UTC](https://discourse.julialang.org/t/nested-loops/49810/2 "2020-11-08T23:45:36Z")

</div>

I am not entirely sure what you want. The loop you describe does what it is expected to do, for each position `(i, j)` it first set `S[i, j]` value to `-1` and execute the body, then it sets `S[i, j]` to `1` and execute the body.

Do you want to run the body for every possible matrix of values `1` and `-1`? If so, I would recommend looking at [`Combinatorics.jl`](https://github.com/JuliaMath/Combinatorics.jl).

---

<div class="post-metadata">

**Author:** ![caetanotc](https://avatars.discourse-cdn.com/v4/letter/c/ce73a5/32.png) [@caetanotc](https://discourse.julialang.org/u/caetanotc)\
**Post date:** [November 9, 2020, 12:53am UTC](https://discourse.julialang.org/t/nested-loops/49810/3 "2020-11-09T00:53:50Z")

</div>

> [@Henrique\_Becker](#):
>
> Do you want to run the body for every possible matrix of values `1` and `-1` ?

Yep, I want to run on all the 2^(N\*N) possibilities, so I would need N^2 nested loops with [-1,1].

> [@Henrique\_Becker](#):
>
> If so, I would recommend looking at [`Combinatorics.jl`](https://github.com/JuliaMath/Combinatorics.jl).

I’ll take a look, thanks for the recommendation.

Edit: N^2 not N^N

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [November 9, 2020, 9:44pm UTC](https://discourse.julialang.org/t/nested-loops/49810/4 "2020-11-09T21:44:08Z")

</div>

If I’m getting this right, it sounds like you want all `NxN` matrices `S` for which `Sᵢⱼ ∈ {-1, 1}`. You can create these in a loop over `i in 2^(N*N)` using the binary representation of `i` to set the values of `S`.

```julia
S′ = ones(Int, N, N)
digs = zeros(Bool, N*N) # a vector to hold the bit representation of `i`.
for i in 1:2^(N*N)
    digits!(digs, i-1, base=2)
    S = S′ .- 2reshape(digs, size(S′))
    # use S
end

```

Above I use two different matrices for the process, but that’s not important. You can just as easily do the whole thing in-place if you preferred. Note that it’s easy for `2^(N*N)` to overflow (you literally can’t go past `N=7`…). You can use `big(2)^(N*N)` to get around that, but at that point the problem would be intractable anyway.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [November 9, 2020, 9:57pm UTC](https://discourse.julialang.org/t/nested-loops/49810/5 "2020-11-09T21:57:31Z")

</div>

Turns out you can also use `multiset_permutations` from `Combinatorics`. The input set needs N² `1` values and the same number of `-1` values. Choosing any N² subset of these (in every permutation) will give the same as the above solution.

```julia
for perm in multiset_permutations([ones(Int, N^2); -ones(Int, N^2)], N^2)
    S = reshape(perm, N, N)
end
```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 9, 2020, 10:20pm UTC](https://discourse.julialang.org/t/nested-loops/49810/6 "2020-11-09T22:20:05Z")

</div>

> [@caetanotc](#):
>
> But I need to compute N in the range 2:5, and doing like that I would need 1 code for each N. Is there a way to optimize that, so I can use just one program?

In addition to the combinatorics solutions suggested above, I also want to point out the [Cartesian indices](https://docs.julialang.org/en/v1/manual/arrays/#Cartesian-indices) facilities in Base and the lower-level [`@nloops`](https://docs.julialang.org/en/v1/devdocs/cartesian/#Base.Cartesian.@nloops) macro. These allow you to effectively have `N` efficient nested loops in such a way that the code is generic over `N`.
