Comparing cell arrays for same values?

조회 수: 3 (최근 30일)
Craig
Craig 2013년 9월 10일
Ok so - This may be a little hard to explain to forgive me.
I have a selection of array of letters (lets call is matrix X), e.g
A B C; A C D; A D G; A E H; Q N J; A G K; A H I; A I W;
Defined as a 8x3 cell (in this case). I also have another array (matrix Y) (11x2 cell) of similar format (populated with letters).
What I want to do is write some code which looks through each row of matrix X in turn and returns a "1" if the row contains both letters in any row of matrix Y and a "0" if not.
For example, matrix X is as above and matrix Y is,
A F; E D; E C; J I; J H; M L; V T; V U; Q J; Q O; Q P;
In this case, the 5th row of matrix X would agree with the statement as "Q" and "N" are present (as in the 9th row of matrix Y). The other rows in X are all fine and would return "1".
I hope that makes sense - happy to explain further if necc.
Best,
Craig
  댓글 수: 2
Azzi Abdelmalek
Azzi Abdelmalek 2013년 9월 10일
You mean Q and J in the fifth row of X
Craig
Craig 2013년 9월 10일
Apologies - yes. thanks!

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

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 9월 10일
편집: Azzi Abdelmalek 2013년 9월 10일
Edit
X={'A' 'B' 'C'
'A' 'C' 'D'
'A' 'D' 'G'
'A' 'E' 'H'
'Q' 'N' 'J'
'A' 'G' 'K'
'A' 'H' 'I'
'A' 'I' 'W'}
Y={ 'A' 'F'
'E' 'D'
'E' 'C'
'J' 'I'
'J' 'H'
'M' 'L'
'V' 'T'
'V' 'U'
'Q' 'J'
'Q' 'O'
'Q' 'P'}
n=size(X,1);
out=zeros(n,1)
for k=1:n
out(k,1)=any(all(ismember(Y,X(k,:)),2))
end
  댓글 수: 1
Craig
Craig 2013년 9월 10일
That works nicely thanks. Think I have a few other tweaks to make to my overall project before I am done but this is very useful so thanks again!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by