A is an m x n character array (I think that is the right term. It says m x n char in the workspace under value).
I want to find a certain row depending on the characters. So say I have a string s of size 1 x n. I want something like this:
find(strcmp(s,A(1:m,:)))
I messed around with ismember instead of strcmp a little too. Can't get it right. I only want it to return an indicator if the row matches the string s.
help is appreciated. Thanks.

 채택된 답변

Matt Fig
Matt Fig 2012년 9월 13일
편집: Matt Fig 2012년 9월 13일

1 개 추천

A = ['asdf';'lelr';'wkre';'pope']
idx = all(ismember(A,'lelr'),2)
Now if you need linear indices rather than a logical index, use:
lidx = find(idx)

댓글 수: 1

Jonathan
Jonathan 2019년 1월 29일
This solution will match all strings that contain any of the characters in the string, not unique matches. See my answer below for an example and a soluton to fix this issue.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Jonathan
Jonathan 2019년 1월 29일

1 개 추천

The answer by Loginatorist was incorrect for my problem that is I believe the same as described by the OP. Below is an example of when the solution gives wrong results:
Occupations = ['educator ';'doctor '];
all(ismember(Occupations,'doctor '),2)
all(ismember(Occupations,'educator '),2)
Output:
ans =
5×1 logical array
0
1
ans =
5×1 logical array
1
1
Clearly, the second match is wrong, as doctor contains all the characters contained within educator.
A solution that fixes this is to use cellstr and strcomp:
OccupationsCell = cellstr(Occupations);
strcmp('doctor',OccupationsCell)
strcmp('educator',OccupationsCell)
Output:
ans =
2×1 logical array
0
1
ans =
2×1 logical array
1
0

댓글 수: 1

Other options: use the 'rows' option:
>> Occupations = ['educator ';'doctor '];
>> ismember(Occupations,'doctor ','rows')
ans =
0
1
Use a cell array:
>> ismember(cellstr(Occupations),'doctor')
ans =
0
1

댓글을 달려면 로그인하십시오.

Edward Umpfenbach
Edward Umpfenbach 2012년 9월 13일

0 개 추천

Great. Thanks.
Was just missing the "all" part.

카테고리

도움말 센터File Exchange에서 Cell Arrays에 대해 자세히 알아보기

질문:

2012년 9월 13일

댓글:

2019년 1월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by