# All Vectors with Entries from a Given Array

**URL:** <https://discourse.julialang.org/t/all-vectors-with-entries-from-a-given-array/40679>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [June 3, 2020, 3:14pm UTC](https://discourse.julialang.org/t/all-vectors-with-entries-from-a-given-array/40679 "2020-06-03T15:14:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![python15](https://avatars.discourse-cdn.com/v4/letter/p/ad7895/32.png) [@python15](https://discourse.julialang.org/u/python15)\
**Post date:** [June 3, 2020, 3:14pm UTC](https://discourse.julialang.org/t/all-vectors-with-entries-from-a-given-array/40679/1 "2020-06-03T15:14:38Z")

</div>

Hey!

I have the following question: Given an array of numbers, say `A = [0, 1, 3]`, what is the best way to get all vectors (arrays) of fix length with entries from `A`? So in other words, I want to get all vectors from the cartesian product `A x ... x A`.

I would be happy for any answers! (:

---

<div class="post-metadata">

**Author:** ![laborg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laborg/32/5474_2.png) [@laborg](https://discourse.julialang.org/u/laborg)\
**Post date:** [June 3, 2020, 3:54pm UTC](https://discourse.julialang.org/t/all-vectors-with-entries-from-a-given-array/40679/2 "2020-06-03T15:54:29Z")

</div>

Hi!

You can use Combinatorics.jl for this purpose:

```julia
julia> using Pkg;

julia> Pkg.add("Combinatorics");

julia> using Combinatorics;

julia> collect(permutations([0,1,3]))
6-element Array{Array{Int64,1},1}:
 [0, 1, 3]
 [0, 3, 1]
 [1, 0, 3]
 [1, 3, 0]
 [3, 0, 1]
 [3, 1, 0]

```

---

<div class="post-metadata">

**Author:** ![python15](https://avatars.discourse-cdn.com/v4/letter/p/ad7895/32.png) [@python15](https://discourse.julialang.org/u/python15)\
**Post date:** [June 3, 2020, 4:00pm UTC](https://discourse.julialang.org/t/all-vectors-with-entries-from-a-given-array/40679/3 "2020-06-03T16:00:20Z")

</div>

I’ve seen the `Combinatorics` package before, but that’s not exactly what I was looking for; I also want to obtain arrays in which elements of `A` are contained more than once; for example `[0, 0, 0]`, `[0, 0, 1]`, `[0, 0, 3]`, `[0, 1, 0]`, `[0, 1, 1]`, `[0, 1, 3]` etc.

So the number of arrays should be `lenght(A)^dimension`.

---

<div class="post-metadata">

**Author:** ![mforets](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mforets/32/298_2.png) [@mforets](https://discourse.julialang.org/u/mforets)\
**Post date:** [June 3, 2020, 4:14pm UTC](https://discourse.julialang.org/t/all-vectors-with-entries-from-a-given-array/40679/4 "2020-06-03T16:14:51Z")

</div>

Did you check `Base.Iterators.product`?

```julia
julia> A = [1, 2];

julia> collect(Base.Iterators.product(A, A))
2×2 Array{Tuple{Int64,Int64},2}:
 (1, 1) (1, 2)
 (2, 1) (2, 2)

```

---

<div class="post-metadata">

**Author:** ![laborg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laborg/32/5474_2.png) [@laborg](https://discourse.julialang.org/u/laborg)\
**Post date:** [June 3, 2020, 4:27pm UTC](https://discourse.julialang.org/t/all-vectors-with-entries-from-a-given-array/40679/5 "2020-06-03T16:27:55Z")

</div>

Then this might help:

```julia
julia> collect(Combinatorics.with_replacement_combinations([0,1,3],3))
10-element Array{Array{Int64,1},1}:
 [0, 0, 0]
 [0, 0, 1]
 [0, 0, 3]
 [0, 1, 1]
 [0, 1, 3]
 [0, 3, 3]
 [1, 1, 1]
 [1, 1, 3]
 [1, 3, 3]
 [3, 3, 3]

```

Although this doesn’t satisfy `length(A)^dimension`. Or as mforets has mentioned:

```julia
julia> a = [0,1,3]

julia> collect(Iterators.product(a,a,a))
3×3×3 Array{Tuple{Int64,Int64,Int64},3}:
[:, :, 1] =
 (0, 0, 0) (0, 1, 0) (0, 3, 0)
 (1, 0, 0) (1, 1, 0) (1, 3, 0)
 (3, 0, 0) (3, 1, 0) (3, 3, 0)

[:, :, 2] =
 (0, 0, 1) (0, 1, 1) (0, 3, 1)
 (1, 0, 1) (1, 1, 1) (1, 3, 1)
 (3, 0, 1) (3, 1, 1) (3, 3, 1)

[:, :, 3] =
 (0, 0, 3) (0, 1, 3) (0, 3, 3)
 (1, 0, 3) (1, 1, 3) (1, 3, 3)
 (3, 0, 3) (3, 1, 3) (3, 3, 3)

```

---

<div class="post-metadata">

**Author:** ![python15](https://avatars.discourse-cdn.com/v4/letter/p/ad7895/32.png) [@python15](https://discourse.julialang.org/u/python15)\
**Post date:** [June 3, 2020, 5:37pm UTC](https://discourse.julialang.org/t/all-vectors-with-entries-from-a-given-array/40679/6 "2020-06-03T17:37:54Z")

</div>

Thanks, that’s what I was looking for!

---

<div class="post-metadata">

**Author:** ![python15](https://avatars.discourse-cdn.com/v4/letter/p/ad7895/32.png) [@python15](https://discourse.julialang.org/u/python15)\
**Post date:** [July 6, 2020, 9:55am UTC](https://discourse.julialang.org/t/all-vectors-with-entries-from-a-given-array/40679/7 "2020-07-06T09:55:13Z")

</div>

Hey!

I have a follow-up question: I want to have the dimension of the vectors as a parameter, like I do not want to write five times `A` as an input in `collect(Base.Iterators.product(A, A, A, A, A))`, and the dimension should be variable. How can I achieve this?

Best,  
python15

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 6, 2020, 12:17pm UTC](https://discourse.julialang.org/t/all-vectors-with-entries-from-a-given-array/40679/8 "2020-07-06T12:17:39Z")

</div>

Something like

```julia
Base.Iterators.product(ntuple(_ -> A, Val(N))...)

```

The function that implements it should use [value types](https://docs.julialang.org/en/v1/manual/types/#%22Value-types%22-1) if possible.
