# Mapslices to tuple of vectors

**URL:** https://discourse.julialang.org/t/mapslices-to-tuple-of-vectors/5460
**Category:** General Usage
**Tags:** question
**Created:** [August 19, 2017, 1:48pm UTC](https://discourse.julialang.org/t/mapslices-to-tuple-of-vectors/5460 "2017-08-19T13:48:28Z")
**Posts on this page:** 3
**Page:** 1

<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: [August 19, 2017, 1:48pm UTC](https://discourse.julialang.org/t/mapslices-to-tuple-of-vectors/5460/1 "2017-08-19T13:48:28Z")

</div>

I have a function that maps a vector to a tuple. I would like to apply this to a matrix by rows, and get a tuple of vectors (`mapslices` gives me a vector of tuples, I tried `zip` but I could not get it working). MWE (inelegant, using the first element):

```julia
function maprows(f, A)
    B = vec(mapslices(f, A, 1))
    ntuple(i->map(x->x[i], B), length(B[1]))
end
f(x) = (x[1:2], x[3], x[4:5]) # number of elements is not necessarily 3
A = ones(10, 5)
C = maprows(f, A)

```

Notice that `C` is a

```julia
Tuple{Array{Array{Float64,1},1},
      Array{Float64,1},
      Array{Array{Float64,1},1}}

```

---

<div class="post-metadata">

### Author: ![klacru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klacru/32/27890_2.png) [@klacru](https://discourse.julialang.org/u/klacru)
#### Post date: [August 19, 2017, 4:17pm UTC](https://discourse.julialang.org/t/mapslices-to-tuple-of-vectors/5460/2 "2017-08-19T16:17:04Z")

</div>

What is your question? Does your `maprows` function what you expect?  
A way to convert the output of mapslices to a tuple of vectors would be for example:

```julia
julia> maprows(f, A) = tuple(collect.(mapslices(f, A, 1))...)
maprows (generic function with 1 method)

julia> C = maprows(f, A)
(Any[[1.0, 1.0], 1.0, [1.0, 1.0]],
 Any[[1.0, 1.0], 1.0, [1.0, 1.0]],
 Any[[1.0, 1.0], 1.0, [1.0, 1.0]],
 Any[[1.0, 1.0], 1.0, [1.0, 1.0]],
 Any[[1.0, 1.0], 1.0, [1.0, 1.0]])

```

---

<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: [August 20, 2017, 6:59am UTC](https://discourse.julialang.org/t/mapslices-to-tuple-of-vectors/5460/3 "2017-08-20T06:59:47Z")

</div>

> [@klacru](#):
>
> What is your question? Does your maprows function what you expect?

It does, the question is about doing it better (more idiomatically). The function you proposed does something completely different.
