For Programmers: Free Programming Magazines  


Home > Archive > Fortran > May 2004 > Maxloc - multiple maxima









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Maxloc - multiple maxima
Roy Plotnick

2004-05-19, 2:32 pm

For a simulation, I need to need the array location of the maximum
value. I have been using MAXLOC for this. I just realized that is
always picks the first element in the array having the maximum value;
this produces a bias in the simulation if more than two equal maximum
values exist. Any suggestions of how to get around this? The arrays
I need to query are small.
-Roy Plotnick
beliavsky@aol.com

2004-05-19, 4:32 pm


plotnick@uic.edu (Roy Plotnick) wrote:
>For a simulation, I need to need the array location of the maximum
>value. I have been using MAXLOC for this. I just realized that is
>always picks the first element in the array having the maximum value;
>this produces a bias in the simulation if more than two equal maximum
>values exist. Any suggestions of how to get around this? The arrays
>I need to query are small.
>-Roy Plotnick


I assume your array contains elements such as integers for which exact equality
can be tested. If so, store the positions of all the elements having the
maximum value. Then use an integer random number generator to choose a position.
If you have N maxima, you need to generate an integer between 1 and N. One
way to do this is

call random_number(x)
i = int(x*N) + 1



----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Paul Van Delst

2004-05-19, 4:32 pm

Roy Plotnick wrote:
> For a simulation, I need to need the array location of the maximum
> value. I have been using MAXLOC for this. I just realized that is
> always picks the first element in the array having the maximum value;
> this produces a bias in the simulation if more than two equal maximum
> values exist. Any suggestions of how to get around this? The arrays
> I need to query are small.


Typically I use MAXVAL to find the maximum value and then COUNT and PACK to determine
where they are, e.g. using integer arrays to avoid floating point comparison messiness,

INTEGER, DIMENSION( 1000 ) :: x
INTEGER :: MaxX, nMaxX, i
INTEGER, DIMENSION(:), ALLOCATABLE :: AllMaxLoc

! -- Get the maximum value in an array
MaxX = MAXVAL( x )

! -- How many are there?
nMaxX = COUNT( x == MaxX )

! -- Determine the array indices of the maximum values
ALLOCATE( AllMaxLoc( nMaxX ) )
AllMaxLoc = PACK( (/ (i, i=1,SIZE(x)) /), x == MaxX )

There are probably syntax errors in the above, but hopefully you get the idea. The only
issue with the samne procedure for floating point arrays is the mask "x == MaxX" might
need to be ( ABS(x-MaxX) > Tolerance ) or summat like that, where you select/compute the
appropriate tolerance value.

cheers,

paulv

Roy Plotnick

2004-05-20, 11:34 am

Thanks for your suggestions - I think a combination of them is what I will use.
- Roy
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com