You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
map or broadcasting syntax takes multidimensional arrays as just 1 dimensional sequences. mapslices always tries to return an array of the same number of dimensions as the taken array, this sometimes causes DimensionMismatch or make a virtual 1 dimensional array extending along a strange axis. There is no specific way to map a function taking an array on a multidimensional array.
a =zeros(2, 2, 5)
"some processing that takes one 2d array and transform it to a 3d array"someprocess(a::AbstractArray{<:Any, 2}) =rand(3, 3, 3)
map(someprocess, a)
#ERROR: MethodError: no method matching someprocess(::Float64)mapslices(someprocess, a, [1, 2])
#ERROR: DimensionMismatch("tried to assign 3 elements to 2 destinations")
Another range expression that acts like a dial knob(hereinafter, i refer to it as --) may manage it.(Maybe the new subtype of AbstractArray that represents the split multidimensional array will be needed for the return value type of getindex)
a[:, :, --]
# the return value behaves like [a[:, :, 1], a[:, :, 2], a[:, :, 3], a[:, :, 4], a[:, :, 5]]. (or simply is it)
a[:, :, 2--3]
# the return value behaves like [a[:, :, 2], a[:, :, 3]]. (or simply is it)map(someprocess, a[:, :, --]) # andsomeprocess.(a[:, :, 2--3]) # will be available
The text was updated successfully, but these errors were encountered:
Hi @takagiy — welcome! You may be interested in the JuliennedArrays package. It takes an array and returns an iterable that yields slices of it. Instead of --, it uses * to specify the "dial knob."
using JuliennedArrays
map(someprocess, julienne(Views, a, (:, :, *)))
someprocess.(julienne(Views, a, (:, :, *)))
I'm going to close this in favor of some of the existing discussions like #3893 and #23645 to make sure our thoughts on the subject don't get too scattered.
map
or broadcasting syntax takes multidimensional arrays as just 1 dimensional sequences.mapslices
always tries to return an array of the same number of dimensions as the taken array, this sometimes causesDimensionMismatch
or make a virtual 1 dimensional array extending along a strange axis. There is no specific way to map a function taking an array on a multidimensional array.Another range expression that acts like a dial knob(hereinafter, i refer to it as
--
) may manage it.(Maybe the new subtype ofAbstractArray
that represents the split multidimensional array will be needed for the return value type ofgetindex
)The text was updated successfully, but these errors were encountered: