I want to find whether code [1 1 -1 -1] is present in a cell C in which C{1}=[1 1 1 1],C{2}=[1 1 -1 -1],C{3}=[1 -1 1 -1],C{4}=[1 -1 -1 1]. Also find out what is the location of the code.

 채택된 답변

Evan
Evan 2015년 2월 18일
편집: Evan 2015년 2월 18일

1 개 추천

key = [1 1 -1 -1]
C{1} = [1 1 1 1];C{2}=[1 1 -1 -1];C{3}=[1 -1 1 -1];C{4}=[1 -1 -1 1]
loc = find(cellfun(@(x)all(x == key),C));
You could also do this with a for loop, checking each cell of C one iteration at a time. cellfun effectively does this.

댓글 수: 4

ankit
ankit 2015년 2월 18일
thankyou sir. Also sir, what should I do if I want to search for a) [1 1] in C b) [1 1 -1 -1 1 1 -1 -1] is containing elements which is a part of C
Stephen23
Stephen23 2015년 2월 18일
편집: Stephen23 2015년 2월 18일
Try using strfind (yes, it also works on numeric arrays!).
ankit
ankit 2015년 2월 18일
when i'm using strfind, there is an error showing as "If any of the input arguments are cell arrays, the first must be a cell array of strings and the second must be a character array." i'm using mat2cell([1 1 -1 -1]) and then comparing with C
Stephen23
Stephen23 2015년 2월 19일
편집: Stephen23 2015년 2월 19일
strfind works on numeric arrays, and not on cell arrays containing numeric arrays. Try it with a simple numeric vectors first to see how it works. Although you have already accepted Evan's answer, this might be more what you are wanting:
cellfun(@(c)strfind(c,key), C)
Or if there might be multiple matches / non-matched entries:
cellfun(@(c)strfind(c,key), C, 'UniformOutput',false)

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

추가 답변 (0개)

카테고리

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

태그

질문:

2015년 2월 18일

편집:

2015년 2월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by