Is there a way to search strings in a cell array similar to numeric arrays?
a = [1 2 3 4 5 6];
>> idx = find(a==3)
idx = 3
>> b = {'1' '2' '3' '4' '5' '6'};
>> idx = find(b=='3')
Undefined function 'eq' for input arguments of type 'cell'.

 採用された回答

Image Analyst
Image Analyst 2016 年 10 月 14 日

5 投票

Use ismember to search cell arrays:
b = {'1' '2' '3' '4' '5' '6'};
logicalIndex = ismember(b, '3') % Or...
actualIndex = find(ismember(b, '3'))

その他の回答 (3 件)

Ganesh Hegade
Ganesh Hegade 2016 年 10 月 14 日

4 投票

Hi, You can use this
strcmp(b, '3');

1 件のコメント

matuser123
matuser123 2016 年 10 月 14 日
Great! Thanks.
find(strcmp(b,'3')==1)

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

michio
michio 2016 年 10 月 14 日

1 投票

Using cellfun is one way.
b = {'1' '2' '3' '4' '5' '6'};
cellfun(@(x) strcmp(x,'3'), b)

1 件のコメント

michio
michio 2016 年 10 月 14 日
Aha, strcmp does accept cell array. Thank Ganesh.

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

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 7 月 4 日

0 投票

Now, what michio suggested works perfectly ok:
b = {'1' '2' '3' '4' '5' '6'};
b(cellfun(@(x) strcmp(x,'3'), b))={'Found 3'}
So this is another good solution for this exercise.

カテゴリ

ヘルプ センター および File Exchange で Characters and Strings についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by