| View previous topic :: View next topic |
| Author |
Message |
Mike Guest
|
Posted: Sat Oct 04, 2008 2:11 am Post subject: search an ordered table |
|
|
Hi
Does anybody know if there is a subroutine or function in IMSL
that can return an index from an ordered array xx from a given value
x? I mean return a value J that x is between xx(J) and xx(J+1).
I surf the IMSL and find only SRCH. Its example is:
USE SRCH_INT
USE UMACH_INT
IMPLICIT NONE
INTEGER N
PARAMETER (N=16)
!
INTEGER INDEX, NOUT
REAL VALUE, X(N)
!
DATA X/61.0, 87.0, 154.0, 170.0, 275.0, 426.0, 503.0, 509.0, &
512.0, 612.0, 653.0, 677.0, 703.0, 765.0, 897.0, 908.0/
!
VALUE = 653.0
CALL SRCH (VALUE, X, INDEX)
!
CALL UMACH (2, NOUT)
WRITE (NOUT,*) 'INDEX = ', INDEX
END
Output
INDEX = 11
but if I change VALUE to 643, it returns -11.
Mike |
|
| |
|
Back to top |
adda Guest
|
Posted: Sat Oct 04, 2008 6:48 pm Post subject: Re: search an ordered table |
|
|
"Mike" <SulfateIon@gmail.com> wrote in message
news:72b8c27a-a18c-42d3-8708-14877f812209@c36g2000prc.googlegroups.com...
[quote]Hi
Does anybody know if there is a subroutine or function in IMSL
that can return an index from an ordered array xx from a given value
x? I mean return a value J that x is between xx(J) and xx(J+1).
[/quote]
You could write one by yourself. If I understand your question correctly,
do j = 1,n-1 !! where n is number of elements of array xx
if(x.le.xx(j)) cycle
if(x.lt.xx(j+1)) return
end do
Cheers |
|
| |
|
Back to top |
Helmut Jarausch Guest
|
Posted: Sat Oct 04, 2008 7:09 pm Post subject: Re: search an ordered table |
|
|
Mike wrote:
[quote]Hi
Does anybody know if there is a subroutine or function in IMSL
that can return an index from an ordered array xx from a given value
x? I mean return a value J that x is between xx(J) and xx(J+1).
I surf the IMSL and find only SRCH. Its example is:
[/quote]
Look for "binary search". But if you program it yourself give attention
to the "border cases". Better take tested code.
--
Helmut Jarausch
Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany |
|
| |
|
Back to top |
homer Guest
|
Posted: Tue Oct 14, 2008 12:45 pm Post subject: Re: search an ordered table |
|
|
Mike,
The IMSL routine is doing what you need (even if you change VALUE to
643 as you describe). Negative return values from SRCH indicate VALUE
falls *between* elements of X, positive return values indicate VALUE
is *present* in X. See the description of INDEX in the IMSL
documentation for SRCH:
INDEX -- Index of Y pointing to VALUE. (Output)
If INDEX is positive, VALUE is found in Y. If INDEX is negative, VALUE
is not found in Y.
INDEX Location of VALUE
1 thru N VALUE = Y(INDEX)
-1 VALUE < Y(1) or N = 0
INDEX Location of VALUE
-N thru -2 Y(-INDEX - 1) < VALUE < Y(INDEX)
-(N + 1) VALUE > Y(N)
hth,
-Homer |
|
| |
|
Back to top |
Mike Guest
|
Posted: Wed Oct 15, 2008 12:33 am Post subject: Re: search an ordered table |
|
|
On Oct 14, 8:45 pm, homer <buddy10...@hotmail.com> wrote:
[quote]Mike,
The IMSL routine is doing what you need (even if you change VALUE to
643 as you describe). Negative return values from SRCH indicate VALUE
falls *between* elements of X, positive return values indicate VALUE
is *present* in X. See the description of INDEX in the IMSL
documentation for SRCH:
INDEX -- Index of Y pointing to VALUE. (Output)
If INDEX is positive, VALUE is found in Y. If INDEX is negative, VALUE
is not found in Y.
INDEX Location of VALUE
1 thru N VALUE = Y(INDEX)
-1 VALUE < Y(1) or N = 0
INDEX Location of VALUE
-N thru -2 Y(-INDEX - 1) < VALUE < Y(INDEX)
-(N + 1) VALUE > Y(N)
hth,
-Homer
[/quote]
Thank you very much for your information.
But I think it should be
[quote]-N thru -2 Y(-INDEX - 1) < VALUE < Y(-INDEX)
[/quote]
Mike |
|
| |
|
Back to top |
|