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'
?
x = strmatch(' str ',STRS) looks through the rows of the character array or cell array of strings STRS to find strings that begin with string str , returning the matching row indices. strmatch is fastest when STRS is a character array.
Description. tf = iscell( A ) returns 1 ( true ) if A is a cell array. Otherwise, it returns 0 ( false ).
String arrays are supported throughout MATLAB® and MathWorks® products. Therefore it is recommended that you use string arrays instead of cell arrays of character vectors. (However, MATLAB functions that accept string arrays as inputs do accept character vectors and cell arrays of character vectors as well.)
k = findstr( str1,str2 ) searches the longer of the two input arguments for any occurrences of the shorter argument and returns the starting index of each occurrence. If it finds no occurrences, then findstr returns the empty array, [] . The input arguments str1 and str2 can be character vectors or string scalars.
I guess the following code could do the trick:
strs = {'HA' 'KU' 'LA' 'MA' 'TATA'} ind=find(ismember(strs,'KU'))
This returns
ans = 2
>> 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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With