MATLAB: Problem with find function

findstring

Hello everyone, i am having two vectors and i want to find the index by doing so but it gives me an error. However it works fine when i do like this for numbers. Any help guys.
hbs_ort = {'North' 'North' 'South' 'South' 'East' 'West' 'West' 'East'}
cabs_ort = {'North'}
match=find(cabs_ort==hbs_ort)

Best Answer

You can use strcmp to obtain the logical index (which often faster and more convenient to use than subscript indices):
>> hbs_ort = {'North' 'North' 'South' 'South' 'East' 'West' 'West' 'East'};
>> cabs_ort = {'North'};
>> X = strcmp(hbs_ort,cabs_ort) % logical index
X =
1 1 0 0 0 0 0 0
If you really need to subscripts:
>> find(X)
ans =
1 2
EDIT: if cabs_ort contains multiple values, then use ismember instead:
>> hbs_ort = {'North' 'North' 'South' 'South' 'East' 'West' 'West' 'East'};
>> cabs_ort = {'North','South'};
>> X = ismember(hbs_ort,cabs_ort)
X =
1 1 1 1 0 0 0 0