Using ismember to seek out pairs

Currently messing with some code which I can't quite get to do what I want.
I have A = [1 2 5; 4 5 7; 7 8 9; 4 7 10; 6 7 2] and B = [2 3; 4 9; 7 9; 7 6]
I want b to be compared with a and using ismember and indicate all the rows that both values in each row in b exist.
So here the answer would be
ans = [0; 0; 1; 0; 1]
The 3rd and 5th rows in A contain a whole row in B so a 1 is returned.
At the moment I am trying, ismember(A,B) but this returns
ans =
0 1 0
1 0 1
1 0 1
1 1 0
1 1 1
as ismember simply return a 1 when any value of B is found in A. Any idea how to use ismember (or otherwise) to do what I am needing?
Thanks

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 9월 18일
편집: Azzi Abdelmalek 2013년 9월 18일

0 개 추천

A = [1 2 5; 4 5 7; 7 8 9; 4 7 10; 6 7 2]
B = [2 3; 4 9; 7 9; 7 6]
for k=1:size(A,1)
out(k)=any(all(ismember(B,A(k,:)),2));
end
out
%or
out=arrayfun(@(x) any(all(ismember(B,A(x,:)),2)),1:size(A,1))

댓글 수: 5

Craig
Craig 2013년 9월 18일
If I have a cell X (1 by 10) where A=X{1} how could I edit your code to loop through each double in the cell and return a cell of all the returns?
Azzi Abdelmalek
Azzi Abdelmalek 2013년 9월 18일
This is not clear
Craig
Craig 2013년 9월 18일
So I want to use your code to perform this several times.
If I have a cell X that contains several arrays (lets say the first is A). How would I adapt your code into a loop to run through one array after another?
Do you mean
y={[1 2 5; 4 5 7; 4 7 10; 6 7 2;44 1 7],[1 2 5; 4 5 7; 7 8 9; 4 7 10; 6 7 2]}
B = [2 3; 4 9; 7 9; 7 6;44 7;11 12;1 2]
for k=1:numel(y)
A=y{k}
out{k}=arrayfun(@(x) any(all(ismember(B,A(x,:)),2)),1:size(A,1));
end
celldisp(out)
Craig
Craig 2013년 9월 18일
Perfect thanks alot!

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

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2013년 9월 18일
편집: Andrei Bobrov 2013년 9월 18일

0 개 추천

w = bsxfun(@eq,reshape(A',[],1),reshape(B',1,[]));
out = any(blockproc(w,[3 2],@(x)sum(x.data(:)))==2,2);
or
w = bsxfun(@eq,reshape(A',[],1),reshape(B',1,[]));
z = conv2(w + 0,ones(3,2));
out = any(z(3:3:end,2:2:end)==2,2);

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by