The code for slicedim()
is just (from base/abstractarraymath.jl):
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:
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 and be able to avoid allocating that temporary array. That is, you can do:
y[:, :, 2] .= softplus.(@view y[:, :, 2])
which will not allocate any temporary arrays. Or, with the hypothetical slicedimview()
function, you could do:
slicedimview(y, 3, 2) .= softplus.(slicedimview(y, 3, 2))
and also not allocate any temporary arrays.