# Making mapslices drop dimensions for scalars results automatially

**URL:** <https://discourse.julialang.org/t/making-mapslices-drop-dimensions-for-scalars-results-automatially/19624>\
**Category:** General Usage\
**Tags:** question, mapslices\
**Created:** [January 14, 2019, 4:30pm UTC](https://discourse.julialang.org/t/making-mapslices-drop-dimensions-for-scalars-results-automatially/19624 "2019-01-14T16:30:18Z")\
**Posts on this page:** 4\
**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:** [January 14, 2019, 4:30pm UTC](https://discourse.julialang.org/t/making-mapslices-drop-dimensions-for-scalars-results-automatially/19624/1 "2019-01-14T16:30:18Z")

</div>

In a sense, I am looking for a workaround for [#16606](https://github.com/JuliaLang/julia/issues/16606): for functions which return an array, I would like to keep it as is, but for functions which return a scalar, I would like to drop the relevant dimension. I can’t just apply `dropdims` automatically, eg

```julia
using Statistics
R = randn(100, 5)
mapslices(col -> quantile(col, [0.25, 0.5, 0.75]), R; dims = 1) # size (3, 5), OK
mapslices(col -> quantile(col, [0.5]), R; dims = 1) # size (1, 5), OK
mapslices(mean, R; dims = 1) # size (1, 5), I want (5, )

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [January 14, 2019, 5:17pm UTC](https://discourse.julialang.org/t/making-mapslices-drop-dimensions-for-scalars-results-automatially/19624/2 "2019-01-14T17:17:26Z")

</div>

On Julia 1.1, you can use `map(mean, eachslice(R, dims=1))`. Now, that won’t do the concatenating behavior in the other cases (it’ll make an array of arrays), but I find that much easier to reason about. 🙂

---

<div class="post-metadata">

**Author:** ![dylanfesta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dylanfesta/32/5125_2.png) [@dylanfesta](https://discourse.julialang.org/u/dylanfesta)\
**Post date:** [October 22, 2019, 3:02pm UTC](https://discourse.julialang.org/t/making-mapslices-drop-dimensions-for-scalars-results-automatially/19624/3 "2019-10-22T15:02:30Z")

</div>

This answer is incorrect. The functions `mapslices` and `eachslice` seem to work differently (and inconsistently?). Note how I need to change dimension to obtain the same result.

```julia-repl

julia> mat = rand(3,4);

julia> map(sum , eachslice(mat ; dims=1))
3-element Array{Float64,1}:
 2.2826198339269235
 2.681128756495633 
 2.108886303861928

julia> dropdims(mapslices(sum, mat ; dims=2); dims = 2 )
3-element Array{Float64,1}:
 2.2826198339269235
 2.681128756495633 
 2.108886303861928 

```

In case of arrays of 3 dimensions or more, I am not sure it’s possible to find the equivalent form of eachslice.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [October 22, 2019, 6:42pm UTC](https://discourse.julialang.org/t/making-mapslices-drop-dimensions-for-scalars-results-automatially/19624/4 "2019-10-22T18:42:25Z")

</div>

For doing many dimensions you may want JuliennedArrays, which appears to follow the convention of `mapslices`:

```julia
julia> using JuliennedArrays

julia> map(sum, Slices(mat, 2)) == map(sum, eachslice(mat; dims=1))
true

julia> map(sum, Slices(rand(10,20,30,40), 1,3)) |> size
(20, 40)

```
