octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: binary lookup


From: Jaroslav Hajek
Subject: Re: binary lookup
Date: Mon, 17 Mar 2008 08:31:55 +0100

>
>  Otherwise, I'm not familiar with this code, so someone else will have
>  to comment about whether this change should be included.
>
>  jwe
>

Hi John,

what about this one? I've rewritten it using STL algorithms. The new
code seems cleaner (at least to those used to STL a little).

I have also got the following two comments to lookup:

1. The current version of lookup (as well as the version I'm
submitting) uses right-exclusive intervals, i.e. lookup ([1 2 3], 3)
returns 3.
If lookup is there primarily for interpolation purposes, perhaps it
would be more sensible
to return 2 in this case (the last subinterval would be
right-inclusive), to avoid classifying 3 as an outlier. However, the
current (more consistent) behavior may be of interest in other cases,
so perhaps there should be an option for this. OTOH, in the
inerpolation routines interpXX,
lookup is used to return only indices in 1:n-1 anyway, so perhaps this
is useless.

2. In a number of places in interp1, interp2 etc, lookup is used in
the following way:
lookup (x(2:end-1), xi) to restrict values to 1:n-1 range, i.e. to
extend the leftmost and rightmost interval to infinity. This is also
advised in lookup's help. While itself a neat trick, it creates a
potentially surprising corner case if the original table is of length
3.
Consider the call
interp1 ([3 2 1], [2 2 1], 1.5, "linear"), which should return 1.5,
but returns 2 (!)
One can of course use a more robust hack, like
lookup ([Inf * sign(x(1)-x(end)), x(2:end-1)(:)], xi)
or simply
max (1, min (numel (x), lookup (x, xi)))
but that's already becoming hard to read. Perhaps an option could
control lookup to
return only inner interval indices (if table has at least 2 elements).

regards,


-- 
RNDr. Jaroslav Hajek
computing expert
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz

Attachment: binlookup.diff
Description: Text Data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]