2
votes

I have a cell matrix with 2 rows - sorted cell array of different strings and array of numbers. Also I have an example string. It's guarantied that this string appears in the 1st row in the cell array. I want to get index of example's appearance in cell array of strings.

Is there any function in Matlab, that provides solving with logarithmic complexity (something like binary search)?

1
How are the strings ordered? - Daniel
@Daniel, alphabetically - Macaronnos

1 Answers

0
votes

If you look at ismember code (type open ismember), you'll see that it basically

  1. Checks if the array is sorted (by calling issorted);
  2. If not, it sorts the array;
  3. Then it applies binary search.

So you can directly use ismember.

Example:

>> strings = {'a', 'aa', 'be', 'day', 'yes'};
>> [tf, loc ] = ismember('day', strings);
>> loc
loc =
     4

Or maybe modify ismember (save it with another name) to bypass step 1, since you already know your array is sorted.