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');
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에서 Matrices and Arrays에 대해 자세히 알아보기

제품

질문:

2016년 10월 14일

답변:

2021년 7월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by