How to unpack result from extrema function



I have a multi dimensional array and previously used minimum() and maximum() to scale the entries between 0 and 1 independently along one of the dimensions.

arr = randn(3,128,128,1000)
mi = minimum(arr, (2,3,4))
ma = maximum(arr, (2,3,4))
@. arr = (arr - mi) / (ma - mi)

Obviously it would be faster to use extrema instead. The obvious solution unfortunately does not work:

arr = randn(3,128,128,1000)
mi, ma = extrema(arr, (2,3,4))
@. arr = (arr - mi) / (ma - mi)

Because extrema does not return a tuple of arrays (as I would have expected) but an array of tuples. I could come up with the following solution, but it does not really seem like a sane thing to do just to get the extrema in a usable form.

arr = randn(3,128,128,1000)

ex = extrema(arr, (2,3,4))
ex = hcat([collect(i) for i in ex[:,1,1,1]]...)'
mi = reshape(ex[:,1],3,1,1,1)
ma = reshape(ex[:,2],3,1,1,1)

@. arr = (arr - mi) / (ma - mi)

So now to my question: Is there a version of extrema (or a way of calling it) that gives me a tuple of arrays? If this does not exist: What is the best/most readable/ideomatic way of converting the one into the other?

arr = randn(3,128,128,1000)
extr = extrema(arr, (2,3,4))
first.(extr), last.(extr)


I did not think of that!
Thank you so much :slight_smile:

I still think extrema should return a tuple of arrays though :smiley: