Effacer les filtres
Effacer les filtres

Problem with find function

2 vues (au cours des 30 derniers jours)
Aftab Ahmed Khan
Aftab Ahmed Khan le 3 Nov 2015
Modifié(e) : Stephen23 le 3 Nov 2015
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)

Réponse acceptée

Stephen23
Stephen23 le 3 Nov 2015
Modifié(e) : Stephen23 le 3 Nov 2015
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
  4 commentaires
Guillaume
Guillaume le 3 Nov 2015

Possibly, this is what you want:

hbs_ort = {'North' 'North' 'South' 'South' 'East' 'West' 'West' 'East'};
cabs_ort = {'North', 'South'};
ismember(hbs_ort, cabs_ort)
Stephen23
Stephen23 le 3 Nov 2015
Modifié(e) : Stephen23 le 3 Nov 2015
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
Note that the order of the input arguments to ismember is significant: it checks if the elements of A are members of B, where A and B are the first and second inputs respectively.

Connectez-vous pour commenter.

Plus de réponses (1)

TastyPastry
TastyPastry le 3 Nov 2015
Modified code:
hbs_ort = {'North' 'North' 'South' 'South' 'East' 'West' 'West' 'East'};
cabs_ort = 'North';
match=strfind(hbs_ort,cabs_ort);
Returns variable match, which is a 1xn cell array containing vectors where 'North' is found in each cell of hbs_ort.

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by