Using ismember to seek out pairs
Mostra commenti meno recenti
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
Risposta accettata
Più risposte (1)
Andrei Bobrov
il 18 Set 2013
Modificato: Andrei Bobrov
il 18 Set 2013
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);
Categorie
Scopri di più su File Operations in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!