On 3 August 2012 11:18, Jim Vickroy <span dir="ltr"><<a href="mailto:jim.vickroy@noaa.gov" target="_blank">jim.vickroy@noaa.gov</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

Hello everyone,<br>
<br>
I'm trying to determine the 2 greatest values, in a 3-d array, along one<br>
axis.<br>
<br>
Here is an approach:<br>
<br>
# ------------------------------------------------------<br>
# procedure to determine greatest 2 values for 3rd dimension of 3-d<br>
array ...<br>
import numpy, <a href="http://numpy.ma" target="_blank">numpy.ma</a><br>
xcnt, ycnt, zcnt   = 2,3,4 # actual case is (1024, 1024, 8)<br>
p0                 = numpy.empty ((xcnt,ycnt,zcnt))<br>
for z in range (zcnt) : p0[:,:,z] = z*z<br>
zaxis              = 2                                            # max<br>
values to be determined for 3rd axis<br>
p0max              = numpy.max (p0, axis=zaxis)                   # max<br>
values for zaxis<br>
maxindices         = numpy.argmax (p0, axis=zaxis)                #<br>
indices of max values<br>
p1                 = p0.copy()                                    # work<br>
array to scan for 2nd highest values<br>
j, i               = numpy.meshgrid (numpy.arange (ycnt), numpy.arange<br>
(xcnt))<br>
p1[i,j,maxindices] = numpy.NaN                                    # flag<br>
all max values<br>
p1                 = numpy.ma.masked_where (numpy.isnan (p1), p1) # hide<br>
all max values<br>
p1max              = numpy.max (p1, axis=zaxis)                   # 2nd<br>
highest values for zaxis<br>
# additional code to analyze p0max and p1max goes here<br>
# ------------------------------------------------------<br>
<br>
I would appreciate feedback on a simpler approach -- e.g., one that does<br>
not require masked arrays and or use of magic values like NaN.<br>
<br>
Thanks,<br>
-- jv<br>
</blockquote></div><br>Here's a way that only uses argsort and fancy indexing:<div><br></div><div><div>>>>a = np.random.randint(10, size=(3,3,3))</div><div>>>>print a</div><div><br></div><div><div>[[[0 3 8]</div>

<div>  [4 2 8]</div><div>  [8 6 3]]</div><div><br></div><div> [[0 6 7]</div><div>  [0 3 9]</div><div>  [0 9 1]]</div><div><br></div><div> [[7 9 7]</div><div>  [5 2 9]</div><div>  [9 3 3]]]</div></div><div><br></div><div>
>>>am = a.argsort(axis=2)</div>
<div>>>>maxs = a[np.arange(a.shape[0])[:,None], np.arange(a.shape[1])[None], am[:,:,-1]]</div><div>>>>print maxs</div><div><br></div><div><div>[[8 8 8]</div><div> [7 9 9]</div><div> [9 9 9]]</div></div>
<div>
<br></div><div>>>>seconds = a[np.arange(a.shape[0])[:,None], np.arange(a.shape[1])[None], am[:,:,-2]]</div><div>>>>print seconds</div><div><br></div><div><div>[[3 4 6]</div><div> [6 3 1]</div><div> [7 5 3]]</div>

</div><div><br></div><div>And to double check:</div><div><br></div><div>>>>i, j = 0, 1</div><div>>>>l = a[i, j,:]</div><div>>>>print l</div><div><br></div><div><div>[4 2 8]</div></div><div><br>
</div>
<div>>>>print np.max(a[i,j,:]), maxs[i,j]</div><div><br></div><div><div>8 8</div></div><div><br></div><div>>>>print l[np.argsort(l)][-2], second[i,j]</div></div><div><div><br></div><div>4 4</div><div><br>

</div><div>Good luck.</div><div><br></div><div>Angus.</div>-- <br>AJC McMorland<br>Post-doctoral research fellow<br>Neurobiology, University of Pittsburgh<br>
</div>