lookup

Look up values in a sorted table.

Syntax

idx = lookup(table, values)

idx = lookup(table, values, opt)

Inputs

table
A table of sorted real values or strings.
The sorting requirement is not enforced. Only a minimal check is performed to determine the sort direction. An unsorted table will produce an undefined result.
Type: double | cell
Dimension: vector
values
The values to locate in the table.
String values can be either a single string or a cell array of strings.
Type: double | string | cell
Dimension: vector | matrix
opt
The lookup option.
The available options are as follows:
'm'
Returns the indices for values that are members of the table, and zero otherwise.
'b'
Returns boolean 1 result for values that are members of the table.
'l'
Returns 1 for values less than min(table).
Applies to real values only, not strings.
'r'
Returns length(table)-1 for values greater than max(table).
Applies to real values only, not strings.

Outputs

idx
The indices of the ascending table members closest to but not greater than each values element. Or, the indices of the descending table members closest to but not less than each values element.
String data is evaluated using lexicographical comparison and assumes that character cases are not mixed.

Example

idx = lookup([2:20], [5.5, 7, 15.4; 12, 3.8, 18.2])
idx = [Matrix] 2 x 3
 4  6  14
11  2  17