# [Numpy-discussion] (cube max and reduction)

Andre Martel soucoupevolante@yahoo....
Thu Apr 26 06:37:53 CDT 2012

________________________________
From: eat <e.antero.tammi@gmail.com>
To: Discussion of Numerical Python <numpy-discussion@scipy.org>
Sent: Sunday, April 22, 2012 5:54 AM
Subject: Re: [Numpy-discussion] (no subject)

Thanks, this was useful. For a large cube, though, I had to loop through the indices
of the maxima:

for i in np.arange(0, ndx.shape[0]):
C_out[i:, ndx == i]= C_in[(i+1):, ndx== i]

Would there be a way to speed this up (no loop) ?

On Fri, Apr 20, 2012 at 9:15 PM,  <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.
>
Perhaps it would be more straightforward to process via 2D-arrays, like:
In []: C
Out[]:
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]]])
In []: C_in= C.reshape(3, -1).copy()
In []: ndx= C_in.argmax(0)
In []: C_out= C_in[:2, :]
In []: C_out[:, ndx== 0]= C_in[1:, ndx== 0]
In []: C_out[1, ndx== 1]= C_in[2, ndx== 1]
In []: C_out.reshape(2, 3, 4)
Out[]:
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]]])

My 2 cents,
-eat

>_______________________________________________
>NumPy-Discussion mailing list
>NumPy-Discussion@scipy.org
>http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>

_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion
-------------- next part --------------
An HTML attachment was scrubbed...