Is there a `slices` function?

Is there a lazy equivalent to mapslices that returns a sliced view into an array?

We have sortperm and sortslices, but I’d like to get the permutation that puts slices in order. I’d like to accomplish this with sortperm(slices(x, dims=2)).

If x::AbstractArray with ndims(x)=3, then I’d expect slices(x, dims=2) to return y::AbstractMatrix{<:AbstractVector} which shares data with x.

I think you are looking for eachslice(x, dims = 2)

1 Like


I was hoping for something one level more like a view (i.e. y[2] = y[4] would have side effects on x) but this is likely the answer I’m looking for. For the specific use case I presented, sortperm(collect(eachslice(x, dims=2))) should work (I’ll just need to provide my own lt opperator to compare matrices)

You’ll be interested in add Slices array type for eachslice/eachrow/eachcol by simonbyrne · Pull Request #32310 · JuliaLang/julia · GitHub, which adds such a view type.

1 Like

Yes! Thanks! That is exactly what I’m looking for! I hope it lands soon.