# Assign values to \`slicedim\`

**URL:** https://discourse.julialang.org/t/assign-values-to-slicedim/5264
**Category:** General Usage
**Created:** [August 7, 2017, 8:23pm UTC](https://discourse.julialang.org/t/assign-values-to-slicedim/5264 "2017-08-07T20:23:34Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jacobcvt12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobcvt12/32/2695_2.png) [@jacobcvt12](https://discourse.julialang.org/u/jacobcvt12)
#### Post date: [August 7, 2017, 8:23pm UTC](https://discourse.julialang.org/t/assign-values-to-slicedim/5264/1 "2017-08-07T20:23:34Z")

</div>

Is there anyway to assign values to a section of an array?

For example

```julia
using Distributions, StatsFuns

# generate an array
y = rand(Normal(), 4, 3, 2)

# section of y to modified
modify = slicedim(y, 3, 2)

# modify section
modified = softplus.(modify)

# would like to do something like this
slicedim(y, 3, 2) = modified

```

Something like this is obviously possible

```julia
y[:, :, 2] = softplus.(y[:, :, 2])

```

But I like the syntax of the `slicedim` approach to be more “programmatic”

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [August 7, 2017, 9:01pm UTC](https://discourse.julialang.org/t/assign-values-to-slicedim/5264/2 "2017-08-07T21:01:01Z")

</div>

The code for `slicedim()` is just (from base/abstractarraymath.jl):

```julia
function slicedim(A::AbstractArray, d::Integer, i)
    d >= 1 || throw(ArgumentError("dimension must be ≥ 1"))
    nd = ndims(A)
    d > nd && (i == 1 || throw_boundserror(A, (ntuple(k->Colon(),nd)..., ntuple(k->1,d-1-nd)..., i)))
    A[setindex(indices(A), i, d)...]
end

```

You could write your own function `slicedimview` which runs exactly the same code but returns a `view` into the resulting array. Then you could do:

```julia
slicedimview(y, 3, 2) .= modified

```

**However** : Note that by splitting up the code into multiple statements, you’re forcing Julia to create a new temporary array to hold `modified`. If you do the entire computation as a single expression, then you’ll benefit from [dot fusion](https://julialang.org/blog/2017/01/moredots) and be able to avoid allocating that temporary array. That is, you can do:

```julia
y[:, :, 2] .= softplus.(@view y[:, :, 2])

```

which will not allocate any temporary arrays. Or, with the hypothetical `slicedimview()` function, you could do:

```julia
slicedimview(y, 3, 2) .= softplus.(slicedimview(y, 3, 2))

```

and also not allocate any temporary arrays.

---

<div class="post-metadata">

### Author: ![jacobcvt12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobcvt12/32/2695_2.png) [@jacobcvt12](https://discourse.julialang.org/u/jacobcvt12)
#### Post date: [August 8, 2017, 8:33pm UTC](https://discourse.julialang.org/t/assign-values-to-slicedim/5264/3 "2017-08-08T20:33:06Z")

</div>

Thanks! This is what I was looking for.
