Re: [Tutor] maximum value in a Numeric array
> I am trying to get the maximum value in a 2-D array. I can use max but it > returns the 1-D array that the max value is in and I then I need to do max > again on that array to get the single max value. > > There has to be a more straightforward way...I have just not found it. > I could also flatten the array to 1 D first then do max but the array I am > going to be working with is fairly large. Two max() calls seems pretty straightforward to me! It probably is possible to be slightly more efficient, but you will have to look at every value in the array at least once whatever you do. The simple brute force approach is probably the best here: # NOTE: untested code... def maximum(matrix): max = None # not 0 to cope with negative matrices for col in matrix: for elem in col: if not max or (elem > max): max = elem return max This only touches each element once and you can't break out till the end because you don't know that the last elemement won't be biggest. However it might be possible to make it faster by sorting the colums and just comparing the first elements. This is because the sort will be in C rather than Python... But premature optimisation would be pointless if the above works... Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
RE: [Tutor] maximum value in a Numeric array
All, Thanks for the help...I am using the older Numeric 23.4. I have some stuff that cannot use Numarray yet. Numeric does not seam to have the same functionality. Happy Holidays. John Ertl -Original Message- From: Danny Yoo [mailto:[EMAIL PROTECTED] Sent: Friday, December 10, 2004 11:26 To: Ertl, John Cc: [EMAIL PROTECTED] Subject: Re: [Tutor] maximum value in a Numeric array On Fri, 10 Dec 2004, Ertl, John wrote: > I am trying to get the maximum value in a 2-D array. I can use max but > it returns the 1-D array that the max value is in and I then I need to > do max again on that array to get the single max value. > > There has to be a more straightforward way...I have just not found it. > > >>> b = array([[1,2],[3,4]]) > >>> max(b) > array([3, 4]) > >>> c = max(b) > >>> max(c) > 4 Hi John, According to: http://stsdas.stsci.edu/numarray/numarray-1.1.html/node35.html#l2h-108 you can use the 'max()' method of an array: ### >>> import numarray >>> b = numarray.array([[1,2],[3,4]]) >>> b.max() 4 ### Hope this helps! ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] maximum value in a Numeric array
On Fri, 10 Dec 2004, Ertl, John wrote: > I am trying to get the maximum value in a 2-D array. I can use max but > it returns the 1-D array that the max value is in and I then I need to > do max again on that array to get the single max value. > > There has to be a more straightforward way...I have just not found it. > > >>> b = array([[1,2],[3,4]]) > >>> max(b) > array([3, 4]) > >>> c = max(b) > >>> max(c) > 4 Hi John, According to: http://stsdas.stsci.edu/numarray/numarray-1.1.html/node35.html#l2h-108 you can use the 'max()' method of an array: ### >>> import numarray >>> b = numarray.array([[1,2],[3,4]]) >>> b.max() 4 ### Hope this helps! ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] maximum value in a Numeric array
Are you using numarray? If so, there appears to be a max method of an array, so you can try b = array([[1,2],[3,4]]) b.max() Note that your method of finding the max row, then finding the max in the row, will not in general give the correct result. Sequences are compared lexicographically - the first elements are compared, and only if they match will the second elements be compared. Using plain lists: >>> b=[ [3,2], [1,4] ] >>> max(b) [3, 2] >>> max(max(b)) 3 You can use a list comprehension to do what you want >>> max([max(l) for l in b]) 4 or in Python 2.4 you can use a generator expression and avoid creating the intermediate list: >>> max(max(l) for l in b) 4 Kent Ertl, John wrote: All, I am trying to get the maximum value in a 2-D array. I can use max but it returns the 1-D array that the max value is in and I then I need to do max again on that array to get the single max value. There has to be a more straightforward way...I have just not found it. b = array([[1,2],[3,4]]) max(b) array([3, 4]) c = max(b) max(c) 4 I could also flatten the array to 1 D first then do max but the array I am going to be working with is fairly large. Thanks ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
[Tutor] maximum value in a Numeric array
All, I am trying to get the maximum value in a 2-D array. I can use max but it returns the 1-D array that the max value is in and I then I need to do max again on that array to get the single max value. There has to be a more straightforward way...I have just not found it. >>> b = array([[1,2],[3,4]]) >>> max(b) array([3, 4]) >>> c = max(b) >>> max(c) 4 >>> I could also flatten the array to 1 D first then do max but the array I am going to be working with is fairly large. Thanks ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor