Behavior of lookup() on cell array of strings wrong?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Behavior of lookup() on cell array of strings wrong?

murphstein
From the docs, I expect that if table T is a sorted cell array of short strings, such that isequal(T, sort(T)) returns true, and Y is short string NOT in T, then I = lookup(T, Y) should return zero (not found).  Instead, I'm getting back index N, which corresponds to the string that would have followed Y in the sorted list, had Y been present.  

Am I missing an option, misunderstanding lookup(), or encountering a behavioral issue in Octave?  I would really rather not have to do a strcmp(Y, T{IDX}) after every lookup() to make sure I've got a valid index.

Running Octave 4.2.1, installed by HomeBrew , on OSX 10.11.6 (El Capitan).

TIA,
Dan Murphy, murphstein@icloud.com
"History is made at night. Character is what you are in the dark."
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Behavior of lookup() on cell array of strings wrong?

Mike Miller-4
On Wed, Jul 12, 2017 at 10:00:06 -0700, murphstein wrote:
> From the docs, I expect that if table T is a sorted cell array of short
> strings, such that isequal(T, sort(T)) returns true, and Y is short string
> NOT in T, then I = lookup(T, Y) should return zero (not found).  Instead,
> I'm getting back index N, which corresponds to the string that would have
> followed Y in the sorted list, had Y been present.  
>
> Am I missing an option, misunderstanding lookup(), or encountering a
> behavioral issue in Octave?  I would really rather not have to do a
> strcmp(Y, T{IDX}) after every lookup() to make sure I've got a valid index.

Yes, you are misunderstanding and missing an option. The help for lookup
describes the behavior pretty accurately, have you looked at it?

By default lookup does quantization such that if I = lookup(T, Y), then
T(I) <= Y <= T(I+1), with the min set to 0 and the max set to N. The
interpretation here is that there are two implied bounds T(0) == -Inf
and T(N+1) == Inf.

    T = [1 2 3 4 5];
    lookup (T, 1)    => 1
    lookup (T, 3)    => 3
    lookup (T, 10)   => 5
    lookup (T, -2)   => 0
    lookup (T, 2.25) => 2

If you instead want lookup to only do exact matching and return 0 for
any Y that is not exactly in T, then pass the 'm' option.

    lookup (T, 1, 'm')    => 1
    lookup (T, 3, 'm')    => 3
    lookup (T, 10, 'm')   => 0
    lookup (T, -2, 'm')   => 0
    lookup (T, 2.25, 'm') => 0

--
mike

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Behavior of lookup() on cell array of strings wrong?

murphstein
Thank you, you are correct and I clearly misunderstood what I thought I'd read and re-read.
Dan Murphy, murphstein@icloud.com
"History is made at night. Character is what you are in the dark."
Loading...