[SciPy-User] Median element by element

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[SciPy-User] Median element by element

Paolo Zaffino
Hi all,
if I have n 3D matixes with the same shape, is it possible to obtain a
new 3D matrix containing the median values computed element by element
across all the n inputs?
Of course I could run a series of for cycles, but I definitely prefer a
pythonitic way of doing that.

Thanks a lot in advance.

Paolo

_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Median element by element

Jerome Kieffer
On Mon, 23 Apr 2018 10:08:03 +0200
Paolo Zaffino <[hidden email]> wrote:

> Hi all,
> if I have n 3D matixes with the same shape, is it possible to obtain a
> new 3D matrix containing the median values computed element by element
> across all the n inputs?
> Of course I could run a series of for cycles, but I definitely prefer a
> pythonitic way of doing that.

What about allocating a 4D array, copying the n datasets there and
perform numpy.median(axis=1/-1).

There are 2 options:
* unoptimized copy + optimized median along the last axis or
* optimized copy + unoptimized median along first axis.

The former is probably faster but this may vary depending on your dataset size.

Cheers,

--
Jérôme Kieffer
_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user