Skip to content

Instantly share code, notes, and snippets.

@joubertnel
Created November 8, 2020 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joubertnel/061e517c07a0cbfb3f68490a51e3d6ed to your computer and use it in GitHub Desktop.
Save joubertnel/061e517c07a0cbfb3f68490a51e3d6ed to your computer and use it in GitHub Desktop.
multiple nested for loops combined in a single outer loop, forming the cartesian product of the iterables
for i = 1:5, j = 1:2
println((i, j))
end
# yields:
# (1, 1)
# (1, 2)
# (2, 1)
# (2, 2)
# (3, 1)
# (3, 2)
# (4, 1)
# (4, 2)
# (5, 1)
#(5, 2)
for i = 1:3, j = 1:5, k = -2:0
println((i, j, k))
end
# yields:
# (1, 1, -2)
# (1, 1, -1)
# (1, 1, 0)
# (1, 2, -2)
# (1, 2, -1)
# (1, 2, 0)
# (1, 3, -2)
# (1, 3, -1)
# (1, 3, 0)
# (1, 4, -2)
# (1, 4, -1)
# (1, 4, 0)
# (1, 5, -2)
# (1, 5, -1)
# (1, 5, 0)
# (2, 1, -2)
# (2, 1, -1)
# (2, 1, 0)
# (2, 2, -2)
# (2, 2, -1)
# (2, 2, 0)
# (2, 3, -2)
# (2, 3, -1)
# (2, 3, 0)
# (2, 4, -2)
# (2, 4, -1)
# (2, 4, 0)
# (2, 5, -2)
# (2, 5, -1)
# (2, 5, 0)
# (3, 1, -2)
# (3, 1, -1)
# (3, 1, 0)
# (3, 2, -2)
# (3, 2, -1)
# (3, 2, 0)
# (3, 3, -2)
# (3, 3, -1)
# (3, 3, 0)
# (3, 4, -2)
# (3, 4, -1)
# (3, 4, 0)
# (3, 5, -2)
# (3, 5, -1)
# (3, 5, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment