Perl. Locate position of maximum value in an array

Click For Summary
The discussion revolves around finding the maximum value and its index in a Perl array, with the user expressing frustration over the complexity compared to MATLAB. The user provides an example of an array and seeks a way to implement a function similar to MATLAB's max and maxloc. Several participants suggest algorithms for achieving this in Perl, emphasizing that while Perl can handle the task, it may not be as straightforward as in MATLAB. They recommend using the List::Util module for the max function and provide a basic algorithm for finding both the maximum value and its index. The conversation also touches on the user's preference for C++ due to perceived performance benefits, but notes the challenges faced with programming in that language under tight deadlines. Alternatives like Python with NumPy and GNU Octave are mentioned as potential solutions for advanced mathematical computations, highlighting the availability of various libraries in Perl that the user may not be aware of.
Don Carnage
Hi guys.
I need to locate the maximum value in an array in Perl. So let's say it looks as follows:

# Define array
my @a;

# Define inputs in array
a[1]=11;
a[2]=12;
a[3]=13;
a[4]=14;
a[5]=15;
a[6]=13;
a[7]=12;
a[8]=14;

Then I would like to do something like max[@a] given the result "15" and then
maxloc[@a] given the result "5"

The arrays I use have about 100,000-1million elements.

Hope you guys can help..
Peter.

.. and the worst thing is that this is so easy in MATLAB. Damn I hate that the MATLAB license is so expensive!
 
Technology news on Phys.org
Hello,

".. and the worst thing is that this is so easy in MATLAB. Damn I hate that the MATLAB license is so expensive! "

If you can use any language or environment you want, why not use C, C++, or Java? They all have very similar syntax and semantics to MATLAB.

Here's the algorithm to do what you're asking:


Max( array[1...n] : array of integers )

max_value ← 0
max_index ← 0

for i ← 1...n
. . . if array > max_value then
. . . . . . max_value ← array
. . . . . . max_index ← i

return (max_value, max_index)

This is easy to implement in C, C++, Java, and MATLAB. It's easy to implement in PERL, too.

$max_value = 0;
$max_index = 0;

for($i = 0; $i < n;$i ++)
{
. . . if($array > $max_value)
. . . {
. . . . . . $max_index = $i;
. . . . . . $max_value = $array;
. . . }
}

return $max_index; # or $max_value, or an array with both in either order.
 
sub max { @_ = sort { $a <=> $b } @_; pop }

More reasonably, try putting this at the top of your file:

use List::Util qw(max);

This will access the standard perl module "List::Util" and import the method "max", which returns the largest numerical item in a list. There are several other methods in that module you may find useful if you look, including sum, and reduce (foldl). There is a large body of perl modules, including some very useful ones installed by default, and if you're going to be using perl it pays to learn about some of the basic ones.

EDIT: I do note these won't give you the "maxloc" type function you were asking for. However AUMathTutor's method will.
 
Last edited:
Actually, the algorithm I gave only works for positive integers in the array. It can easily be changed, though.

And you don't want to sort it to get the max... though library functions are good to look into.
 
AUMathTutor said:
And you don't want to sort it to get the max...

I think there are several reasons one does not want to use my code sample :)
 
Last edited:
Hi guys.
Thx ALOT! I will try this..

I would love to use C++ instead of Perl! The problem is just that I have to make a script at my work where we have some pretty massive deadlines, and every time I try to do something in C,C++ or FORTRAN I always get my boss on my back because it takes so long. There is always some issue with allocation or something else. Guess the biggest problem is that I am NOT a programmer, but fortunately I will get a Course in C++ in some months so I can finally get a good understanding of pointers and so on. The thing that irritates me the most with Perl is, that there is really no advances math libraries available.. Anyways, life just sucks without MATLAB if you have to do a lot of productive WORK! MATLAB is just 10 times speed of everything else when doing advanced mathematical work IMHO..
 
Don Carnage said:
The thing that irritates me the most with Perl is, that there is really no advances math libraries available..

Are you quite sure? I'd imagine there are a lot of advanced math libraries for perl. What would consider to constitute an "advanced math library"?

If you want to check, the perl module repository is here:

http://search.cpan.org/

I see there's a module for LAPACK, for example.

If it's not essential to stick to Perl specifically I've also previously heard it suggested that people familiar with Matlab are often comfortable using "numpy", or "Numerical Python", which is the Python scripting language packaged with some advanced-math and plotting libraries. There's also something called Octave, which is a free/GNU application similar to Matlab in many ways.
 
Last edited:

Similar threads

  • · Replies 6 ·
Replies
6
Views
11K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
24
Views
2K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
7K