# Permutation / combination of elements of a matrix per row

**URL:** https://discourse.julialang.org/t/permutation-combination-of-elements-of-a-matrix-per-row/32750
**Category:** New to Julia
**Created:** [December 28, 2019, 10:13am UTC](https://discourse.julialang.org/t/permutation-combination-of-elements-of-a-matrix-per-row/32750 "2019-12-28T10:13:25Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![tog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tog/32/11858_2.png) [@tog](https://discourse.julialang.org/u/tog)
#### Post date: [December 28, 2019, 10:13am UTC](https://discourse.julialang.org/t/permutation-combination-of-elements-of-a-matrix-per-row/32750/1 "2019-12-28T10:13:26Z")

</div>

Hi

I have created a M x N matrix from which I want to create all elements resulting from

i.e. if I have A = [11 12 13 14; 21 22 23 24; 31 32 33 34], I want to create:

[11; 21; 31] [11; 21; 32] [11; 21; 33] [11; 21; 34] [12; 21; 31] [12; 21; 32] [12; 21; 33] [12; 21; 34] …

I found

```B = collect(combinations(A, 4)) ````

but that is generating all permutations not only picking an element per row.

Any idea ?

Thanks

---

<div class="post-metadata">

### Author: ![tog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tog/32/11858_2.png) [@tog](https://discourse.julialang.org/u/tog)
#### Post date: [December 28, 2019, 3:50pm UTC](https://discourse.julialang.org/t/permutation-combination-of-elements-of-a-matrix-per-row/32750/2 "2019-12-28T15:50:14Z")

</div>

It seems that:

> collect(Iterators.product(eachcol(A)…))

returns an iterator that will allow to go through tuples

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [December 30, 2019, 11:47am UTC](https://discourse.julialang.org/t/permutation-combination-of-elements-of-a-matrix-per-row/32750/3 "2019-12-30T11:47:32Z")

</div>

I think you mean,

```julia
julia> vec( collect( Iterators.product(eachrow(A)...) ) )
64-element Array{Tuple{Int64,Int64,Int64},1}:
 (11, 21, 31)
 (12, 21, 31)
 (13, 21, 31)
 (14, 21, 31)
 (11, 22, 31)
 (12, 22, 31)
 (13, 22, 31)
 (14, 22, 31)
 (11, 23, 31)
 (12, 23, 31)
 (13, 23, 31)
 (14, 23, 31)
 (11, 24, 31)
 ⋮
 (11, 22, 34)
 (12, 22, 34)
 (13, 22, 34)
 (14, 22, 34)
 (11, 23, 34)
 (12, 23, 34)
 (13, 23, 34)
 (14, 23, 34)
 (11, 24, 34)
 (12, 24, 34)
 (13, 24, 34)
 (14, 24, 34)

```

Or, if you like piping,  
`Iterators.product(eachrow(A)...) |> collect |> vec`
