Navigation

Operators and Keywords

Function List:

C++ API

: idx = lookup (table, y)
: idx = lookup (table, y, opt)

Lookup values in a sorted table.

This function is usually used as a prelude to interpolation.

If table is increasing and idx = lookup (table, y), then table(idx(i)) <= y(i) < table(idx(i+1)) for all y(i) within the table. If y(i) < table(1) then idx(i) is 0. If y(i) >= table(end) or isnan (y(i)) then idx(i) is n.

If the table is decreasing, then the tests are reversed. For non-strictly monotonic tables, empty intervals are always skipped. The result is undefined if table is not monotonic, or if table contains a NaN.

The complexity of the lookup is O(M*log(N)) where N is the size of table and M is the size of y. In the special case when y is also sorted, the complexity is O(min(M*log(N),M+N)).

table and y can also be cell arrays of strings (or y can be a single string). In this case, string lookup is performed using lexicographical comparison.

If opts is specified, it must be a string with letters indicating additional options.

m

table(idx(i)) == val(i) if val(i) occurs in table; otherwise, idx(i) is zero.

b

idx(i) is a logical 1 or 0, indicating whether val(i) is contained in table or not.

l

For numeric lookups the leftmost subinterval shall be extended to infinity (i.e., all indices at least 1)

r

For numeric lookups the rightmost subinterval shall be extended to infinity (i.e., all indices at most n-1).

Package: octave