# Combvec in Julia

**URL:** <https://discourse.julialang.org/t/combvec-in-julia/87485>\
**Category:** New to Julia\
**Created:** [September 19, 2022, 5:12pm UTC](https://discourse.julialang.org/t/combvec-in-julia/87485 "2022-09-19T17:12:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![yoshi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yoshi/32/38259_2.png) [@yoshi](https://discourse.julialang.org/u/yoshi)\
**Post date:** [September 19, 2022, 5:12pm UTC](https://discourse.julialang.org/t/combvec-in-julia/87485/1 "2022-09-19T17:12:06Z")

</div>

Sorry I have a trivial question. In matlab, `combvec` takes as inputs vectors and outputs all combinations of element from each vector. I was looking for a similar function in julia and found one in the [SparseGrids library](https://docs.juliahub.com/SparseGrids/pP1HM/2.0.0/autodocs/#SparseGrids.combvec-Union%7BTuple%7BArray%7BArray%7BT,1%7D,1%7D%7D,%20Tuple%7BT%7D%7D%20where%20T). I expect the following code to yield an array of vectors.

```julia
using SparseGrids
A = Vector(0:1)
vectorCombs = SparseGrids.combvec(A,A,A)

```

However I receive the following error:

```julia
ERROR: MethodError: no method matching combvec(::Vector{Int64})
Closest candidates are:
  combvec(::Array{Vector{T}, 1}) where T at ~/.julia/packages/SparseGrids/P8oxX/src/grids.jl:158
Stacktrace:
 [1] top-level scope
   @ REPL[16]:1

```

I loaded the library, what happened?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [September 19, 2022, 5:13pm UTC](https://discourse.julialang.org/t/combvec-in-julia/87485/2 "2022-09-19T17:13:25Z")

</div>

You might want `Iterators.product`

```julia
julia> a = collect(0:5);

julia> collect(Iterators.product(a, a))
6×6 Matrix{Tuple{Int64, Int64}}:
 (0, 0) (0, 1) (0, 2) (0, 3) (0, 4) (0, 5)
 (1, 0) (1, 1) (1, 2) (1, 3) (1, 4) (1, 5)
 (2, 0) (2, 1) (2, 2) (2, 3) (2, 4) (2, 5)
 (3, 0) (3, 1) (3, 2) (3, 3) (3, 4) (3, 5)
 (4, 0) (4, 1) (4, 2) (4, 3) (4, 4) (4, 5)
 (5, 0) (5, 1) (5, 2) (5, 3) (5, 4) (5, 5)

```

---

<div class="post-metadata">

**Author:** ![yoshi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yoshi/32/38259_2.png) [@yoshi](https://discourse.julialang.org/u/yoshi)\
**Post date:** [September 19, 2022, 5:15pm UTC](https://discourse.julialang.org/t/combvec-in-julia/87485/3 "2022-09-19T17:15:41Z")

</div>

I don’t find the Iterators library:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/3/73d26e96dbec2e1a111d48293001b8ec6ca9c34d.png)

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [September 19, 2022, 5:17pm UTC](https://discourse.julialang.org/t/combvec-in-julia/87485/4 "2022-09-19T17:17:33Z")

</div>

Iterators is in Base, so you don’t need to install it.

---

<div class="post-metadata">

**Author:** ![yoshi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yoshi/32/38259_2.png) [@yoshi](https://discourse.julialang.org/u/yoshi)\
**Post date:** [September 19, 2022, 5:18pm UTC](https://discourse.julialang.org/t/combvec-in-julia/87485/5 "2022-09-19T17:18:38Z")

</div>

bleh, thx

---

<div class="post-metadata">

**Author:** ![ianfiske](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@ianfiske](https://discourse.julialang.org/u/ianfiske)\
**Post date:** [September 19, 2022, 6:07pm UTC](https://discourse.julialang.org/t/combvec-in-julia/87485/6 "2022-09-19T18:07:36Z")

</div>

Also, note that the error you posted suggests that the method `combvec` expects to take an `Array{T, 1}` of `Vectors` – also known as a `Vector{Vector{T}}`.

```julia
julia> using SparseGrids
julia> A = Vector(0:1)
2-element Vector{Int64}:
 0
 1

julia> vectorCombs = SparseGrids.combvec([A,A,A])
8-element Vector{Vector{Int64}}:
 [0, 0, 0]
 [1, 0, 0]
 [0, 1, 0]
 [1, 1, 0]
 [0, 0, 1]
 [1, 0, 1]
 [0, 1, 1]
 [1, 1, 1]

```

And yes, it does use `Iterators.product`: [SparseGrids.jl/grids.jl at d4fca5258321657542143773cd0763d2f03a8b50 · robertdj/SparseGrids.jl · GitHub](https://github.com/robertdj/SparseGrids.jl/blob/d4fca5258321657542143773cd0763d2f03a8b50/src/grids.jl#L160)
