help-octave
[Top][All Lists]

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

 From: Mike Miller Subject: Re: Behavior of lookup() on cell array of strings wrong? Date: Thu, 13 Jul 2017 18:01:46 -0700 User-agent: NeoMutt/20170609 (1.8.3)

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

reply via email to