[Numpy-discussion] (no subject)

Tony Yu tsyu80@gmail....
Fri Apr 20 13:34:05 CDT 2012


On Fri, Apr 20, 2012 at 2:15 PM, Andre Martel <soucoupevolante@yahoo.com>wrote:

> What would be the best way to remove the maximum from a cube and
> "collapse" the remaining elements along the z-axis ?
> For example, I want to reduce Cube to NewCube:
>
> >>> Cube
> array([[[  13,   2,   3,  42],
>         [  5, 100,   7,   8],
>         [  9,   1,  11,  12]],
>
>        [[ 25,   4,  15,   1],
>         [ 17,  30,   9,  20],
>         [ 21,   2,  23,  24]],
>
>        [[ 1,   2,  27,  28],
>         [ 29,  18,  31,  32],
>         [ -1,   3,  35,   4]]])
>
> NewCube
>
> array([[[  13,   2,   3,  1],
>         [  5, 30,   7,   8],
>         [  9,   1,  11,  12]],
>
>        [[ 1,   2,  15,  28],
>         [ 17,  18,  9,  20],
>         [ -1,   2,  23,   4]]])
>
> I tried with argmax() and then roll() and delete() but these
> all work on 1-D arrays only. Thanks.
>
>
Actually, those commands do work with n-dimensional arrays, but you'll have
to specify the axis (the default for all these functions is `axis=None`
which tell the function to operate on flattened the arrays). If you don't
care about the order of the "collapse", you can just do a simple sort (and
drop the last---i.e. max---sub-array):

>>> np.sort(cube, axis=0)[:2]

If you need to keep the order, you can probably use some combination of
`np.argsort` and `np.choose`.

Cheers,
-Tony
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20120420/996c8176/attachment-0001.html 


More information about the NumPy-Discussion mailing list