필터 지우기
필터 지우기

matching texts in two cell arrays

조회 수: 22 (최근 30일)
Quy
Quy 2014년 7월 22일
댓글: Quy 2014년 7월 22일
I have two cell arrays, one longer than the other. Both containing strings. The shorter cell array contain only part of a name of one of the cell in the longer cell array. Can I match each individual cells in the shorter cell arrays to find the index of the full name in the cell array. for example;
shortcellarray = {'Micheal' 'John' 'Eric'};
longcellarray = {'Lisa Ann' 'Michael Adams' 'Art Vara' 'Brian Burns' 'Eric Holmes'};
how do I get the index of the long array from the match of short array (without loop iteration, which I know can be done with strfind on each cell within the shortarray)? The answer should be:
index = {2, [], 5}
Thanks.

채택된 답변

Michael Haderlein
Michael Haderlein 2014년 7월 22일
I'm totally sure that much better solutions will be given, but one reads as:
index = cellfun(@(a) strmatch(a,longcellarray),shortcellarray,'uniform',false)
index =
[2] [0x1 double] [5]
Best regards, Michael
  댓글 수: 2
Michael Haderlein
Michael Haderlein 2014년 7월 22일
(I fixed the first value of shortcellarray to "Michael" instead of "Micheal")
Quy
Quy 2014년 7월 22일
This works great for me.
Thanks for the respond. I think cellfun function looks very useful, and I need to get better at using it.
Thanks.

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

추가 답변 (1개)

Joseph Cheng
Joseph Cheng 2014년 7월 22일
you can do this
shortcellarray = {'Michael' 'John' 'Eric'};
longcellarray = {'Lisa Ann' 'Michael Adams' 'Art Vara' 'Brian Burns' 'Eric Holmes'};
for ind =1:length(shortcellarray)
IndexC = strfind(longcellarray, cell2mat(shortcellarray(ind)));
Index{1,ind} = find(not(cellfun('isempty', IndexC)));
end
the strfind works within cells.
  댓글 수: 1
Quy
Quy 2014년 7월 22일
Using a loop would definitely work, and that is what i have, but I was looking for a better way to do this.
Thanks.

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

카테고리

Help CenterFile 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