eachslice
does the opposite of what I would like. For instance, if X
is a 3D array, then eachslice(X,dims=1)
gives an iterator over 2D matrices. Rather, I would like an iterator over all ‘column vectors’, like eachcol()
. The following code does that for arrays with 3 or fewer dimensions. (It’s straightforward to instead get iterators over rows or ‘aisles’)
Now, my questions is: is there a simple generalisation of this? If so, I think we should make a PR. Suggestions are welcome.
function eachcol3D(A::AbstractArray)
if 1 <= ndims(A) <= 2
z = eachcol(A) #(view(A,:,i) for i in axes(A,2))
elseif ndims(A) == 3
z = (view(A,:,i,j) for i in axes(A,2), j in axes(A,3))
else
error("this function works only on 1D-3D arrays")
end
return z
end