is there a way to get Nd indices of max of Nd array
my case is only for 2d, but should apply to Nd as well. It would be convienent if np.max would return a tuple of the max value and its Nd location indices. Is there an easier way than just using the 1d flattened array max index (np.argmax) and calculating its corresponding Nd location? Chris
On Sun, May 3, 2009 at 3:30 PM, Chris Colbert <sccolbert@gmail.com> wrote:
my case is only for 2d, but should apply to Nd as well.
It would be convienent if np.max would return a tuple of the max value and its Nd location indices.
Is there an easier way than just using the 1d flattened array max index (np.argmax) and calculating its corresponding Nd location?
factors = np.random.randint(5,size=(4,3)) factors array([[1, 1, 3], [0, 2, 1], [4, 4, 1], [2, 2, 4]]) factors.max() 4 np.argmax(factors) 6 np.nonzero(factors==factors.max()) (array([2, 2, 3]), array([0, 1, 2]))
Josef
but this gives me just the locations of the column/row maximums. I need the (x,y) location of the array maximum. Chris On Sun, May 3, 2009 at 4:34 PM, <josef.pktd@gmail.com> wrote:
On Sun, May 3, 2009 at 3:30 PM, Chris Colbert <sccolbert@gmail.com> wrote:
my case is only for 2d, but should apply to Nd as well.
It would be convienent if np.max would return a tuple of the max value and its Nd location indices.
Is there an easier way than just using the 1d flattened array max index (np.argmax) and calculating its corresponding Nd location?
factors = np.random.randint(5,size=(4,3)) factors array([[1, 1, 3], [0, 2, 1], [4, 4, 1], [2, 2, 4]]) factors.max() 4 np.argmax(factors) 6 np.nonzero(factors==factors.max()) (array([2, 2, 3]), array([0, 1, 2]))
Josef _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
wait, nevermind. your're right. Thanks! On Sun, May 3, 2009 at 6:30 PM, Chris Colbert <sccolbert@gmail.com> wrote:
but this gives me just the locations of the column/row maximums.
I need the (x,y) location of the array maximum.
Chris
On Sun, May 3, 2009 at 4:34 PM, <josef.pktd@gmail.com> wrote:
On Sun, May 3, 2009 at 3:30 PM, Chris Colbert <sccolbert@gmail.com> wrote:
my case is only for 2d, but should apply to Nd as well.
It would be convienent if np.max would return a tuple of the max value and its Nd location indices.
Is there an easier way than just using the 1d flattened array max index (np.argmax) and calculating its corresponding Nd location?
factors = np.random.randint(5,size=(4,3)) factors array([[1, 1, 3], [0, 2, 1], [4, 4, 1], [2, 2, 4]]) factors.max() 4 np.argmax(factors) 6 np.nonzero(factors==factors.max()) (array([2, 2, 3]), array([0, 1, 2]))
Josef _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
2009/5/3 <josef.pktd@gmail.com>:
factors = np.random.randint(5,size=(4,3)) factors array([[1, 1, 3], [0, 2, 1], [4, 4, 1], [2, 2, 4]]) factors.max() 4 np.argmax(factors) 6 np.nonzero(factors==factors.max()) (array([2, 2, 3]), array([0, 1, 2]))
Since you have more than one maximum here, you have to do something like Josef outlined above. If you only want the indices of the first maximum argument, you can use np.unravel_index(6, factors.shape) Regards Stéfan
participants (3)
-
Chris Colbert -
josef.pktd@gmail.com -
Stéfan van der Walt