Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Splitting multidimensional array with getindex() #25428

Closed
takagiy opened this issue Jan 6, 2018 · 1 comment
Closed

Splitting multidimensional array with getindex() #25428

takagiy opened this issue Jan 6, 2018 · 1 comment

Comments

@takagiy
Copy link

takagiy commented Jan 6, 2018

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[:, :, --]) # and
someprocess.(a[:, :, 2--3]) # will be available
@mbauman
Copy link
Member

mbauman commented Jan 6, 2018

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.

@mbauman mbauman closed this as completed Jan 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants