AxisArrays, iterate over all dimensions except certain ones?

Is there an easy way to iterate over all axes except certain ones? I.e. the negation of the inputs? My use case is a general function that works on images with an arbitrary number of dimensions, but needs to iterate over all XY slices.

Here’s a super clunky way that I just whipped up to show what I was generally thinking:

using AxisArrays
# this is given to us and the axis number/order is not guaranteed
mat = AxisArray(rand(10, 10, 3, 10), :x, :y, :channel, :time);

all_axs = Set(axisnames(mat))
pop!(all_axs, :x)
pop!(all_axs, :y)
axs = collect(all_axs)
for I in CartesianIndices(Tuple(size(mat, Axis{ax}) for ax in axs))
    println(collect(Axis{ax}(I[i]) for (i, ax) in enumerate(axs)))

What’s a better way to accomplish this?


You could do eachslice twice to iterate the slices directly. But AxisArrays doesn’t support it.

DimensionalData.jl and NamedDims.jl do (but aren’t as polished as AxisArrays in other ways yet). I’ll think about adding multi dim eachslice to DimensionalData.jl it seems useful.

1 Like