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)

 採用された回答

Stephen23
Stephen23 2015 年 11 月 3 日
編集済み: Stephen23 2015 年 11 月 3 日

1 投票

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 件のコメント

Guillaume
Guillaume 2015 年 11 月 3 日
To explain a bit more why you have to use strcmp and why you cannot use == for string comparison:
A string is just a vector of characters, so for example the string 'North' is the same as the vector ['N' 'o' 'r' 't' 'h']. It is a vector with 5 elements. If you think in terms of numbers, it is equivalent for example to [14 15 18 20 8].
Similarly, the string 'East' is the same as the vector ['E' 'a' 's' 't']. An equivalent in numbers could be [5 1 19 20]. The vector has 4 elements.
== compares vectors by comparing element to element. Hence the vectors must have the same size. You can never use it compare an arbitrary string with another arbitrary string, since they may have different length.
strcmp does a string comparison. It returns true if the strings are the same, false otherwise, regardless of their length.
Aftab Ahmed Khan
Aftab Ahmed Khan 2015 年 11 月 3 日
Thank you for the response but if cabs_ort contains two strings and i want to compare that with other vector.
cabs_ort = {'North','South'}
Guillaume
Guillaume 2015 年 11 月 3 日

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 2015 年 11 月 3 日
編集済み: Stephen23 2015 年 11 月 3 日
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.

サインインしてコメントする。

その他の回答 (1 件)

TastyPastry
TastyPastry 2015 年 11 月 3 日

1 投票

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.

カテゴリ

タグ

質問済み:

2015 年 11 月 3 日

編集済み:

2015 年 11 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by