108

Let's say I have the cell array

strs = {'HA' 'KU' 'LA' 'MA' 'TATA'}

What should I do if I want to find the index of 'KU'?

Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122
Benjamin
  • 1,223
  • 2
  • 9
  • 4
  • 8
    Note that the current top answer is quite outdated. Make sure to also check [this answer](http://stackoverflow.com/a/9433684/983722). – Dennis Jaheruddin May 08 '14 at 14:38

8 Answers8

130

I guess the following code could do the trick:

strs = {'HA' 'KU' 'LA' 'MA' 'TATA'}
ind=find(ismember(strs,'KU'))

This returns

ans = 
     2
rayryeng
  • 102,964
  • 22
  • 184
  • 193
Vidar
  • 4,141
  • 5
  • 24
  • 30
  • 13
    Keep in mind this generic solution is good because it works for multiple data types, but it only works for exact matches. If you need a case-insensitive match, see http://stackoverflow.com/a/9433112/44737. If you need to match something more complex like a regex or a field in a structure, see http://stackoverflow.com/a/8061808/44737 – rob Sep 26 '13 at 19:27
  • 1
    ismember is a little more clunky and can get updated with newer versions. I feel more safer with str based functions like strcmpi, etc. – Maddy Apr 23 '15 at 19:32
  • 1
    Works in Octave, too – Nino van Hooff Nov 22 '18 at 13:10
90
>> strs = {'HA' 'KU' 'LA' 'MA' 'TATA'};
>> tic; ind=find(ismember(strs,'KU')); toc

Elapsed time is 0.001976 seconds.

>> tic; find(strcmp('KU', strs)); toc

Elapsed time is 0.000014 seconds.

SO, clearly strcmp('KU', strs) takes much lesser time than ismember(strs,'KU')

Pankaj Gupta
  • 1,019
  • 7
  • 6
  • 5
    Great! Your solution should be the top one! – Antonvh Aug 06 '14 at 08:34
  • 1
    Yes I agree, this is the best solution, however its just a comparison between andreys and vidars solutions. So actually andrey should get it. – Leo Oct 17 '14 at 11:33
  • 1
    Actually, it should be pointed out that this method doesn't work if you are comparing two arrays of different size (i.e. if instead of 'KU' on the left side, you have an array of strings). Vidar's solution does work in that case (quite nicely), so is more general. – Nate Oct 21 '14 at 16:22
  • @pankaj : How does this compare to making a Map of String->Indices and then getting index by doing a map lookup? Asking perf wise. – faizan Oct 01 '16 at 23:33
41

Since 2011a, the recommended way is:

booleanIndex = strcmp('KU', strs)

If you want to get the integer index (which you often don't need), you can use:

integerIndex = find(booleanIndex);

strfind is deprecated, so try not to use it.

Andrey Rubshtein
  • 20,795
  • 11
  • 69
  • 104
25

I see that everybody missed the most important flaw in your code:

strs = {'HA' 'KU' 'LA' 'MA' 'TATA'}

should be:

strs = {'HA' 'KU' 'NA' 'MA' 'TATA'} 

or

strs = {'HAKUNA' 'MATATA'}

Now if you stick to using

ind=find(ismember(strs,'KU'))

You'll have no worries :).

Curt
  • 251
  • 3
  • 3
13

Other answers are probably simpler for this case, but for completeness I thought I would add the use of cellfun with an anonymous function

indices = find(cellfun(@(x) strcmp(x,'KU'), strs))

which has the advantage that you can easily make it case insensitive or use it in cases where you have cell array of structures:

indices = find(cellfun(@(x) strcmpi(x.stringfield,'KU'), strs))
robince
  • 10,826
  • 3
  • 35
  • 48
6

Most shortest code:

strs = {'HA' 'KU' 'LA' 'MA' 'TATA'};
[~,ind]=ismember('KU', strs)

But it returns only first position in strs. If element not found then ind=0.

Maxim Suslov
  • 4,335
  • 1
  • 35
  • 29
5

The strcmp and strcmpi functions are the most direct way to do this. They search through arrays.

strs = {'HA' 'KU' 'LA' 'MA' 'TATA'}
ix = find(strcmp(strs, 'KU'))
Andrew Janke
  • 23,508
  • 5
  • 56
  • 85
-2

did you try

indices = Find(strs, 'KU')

see link

alternatively,

indices = strfind(strs, 'KU');

should also work if I'm not mistaken.

Tom
  • 539
  • 4
  • 7
  • I have tried find but since the array is cell array so MATLAB returns me some errors... But the function strfind seems working, thanks! – Benjamin Nov 09 '11 at 07:09