# how to check if two elements are in a row of a matrix?

조회 수: 24(최근 30일)
Pramod Devireddy 2015년 7월 1일
답변: Andrei Bobrov 2015년 7월 2일
suppose
A=[1 2;
4 5;
6 9]
B=[1 3 5 6;
1 2 4 7;
5 6 4 8;
1 2 3 4].
In this case 1,2 of 1st row of A are present in 2nd row B, similarly all rows of A have to check with rows of B, and create a matrix &nbsp C=[6 9] &nbsp which are not present in any row of B.

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

### 채택된 답변

the cyclist 2015년 7월 2일
Here is one clumsy way:
A=[1 2;
4 5;
6 9];
B=[1 3 5 6;
1 2 4 7;
5 6 4 8;
1 2 3 4];
rowCountA = size(A,1);
rowCountB = size(B,1);
this_row_of_A_is_in_B = false(rowCountA,1);
for nb = 1:rowCountB
this_row_of_A_is_in_B = this_row_of_A_is_in_B | all(ismember(A,B(nb,:)),2);
end
rows_of_A_that_are_not_in_B = A(not(this_row_of_A_is_in_B),:)
##### 댓글 수: 2표시숨기기 이전 댓글 수: 1
the cyclist 2015년 7월 2일
By my understanding of the problem, the output for these inputs should be
[6 9;
1 8]
because there is no row of B that has [6,9] and also no row that has [1,8].
That output is what my code has, so it behaves as I intended, and how I interpreted the question. (But, of course, I could be interpreting incorrectly.)

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

### 추가 답변(2개)

Azzi Abdelmalek 2015년 7월 2일
편집: Azzi Abdelmalek 2015년 7월 2일
A=[1 2;
4 5;
6 9
1 8]
B=[1 3 5 6;
1 2 4 7;
5 6 4 8;
1 2 3 4]
out=[];
for ii=1:size(A,1)
for jj=1:size(B,1)
id=all(ismember(A(ii,:),B(jj,:)));
if id==1
out(end+1,:)=A(ii,:);
break
end
end
end
out=setdiff(A,out,'rows')
##### 댓글 수: 0표시숨기기 이전 댓글 수: -1

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

Andrei Bobrov 2015년 7월 2일
ii = fullfact([size(A,1),size(B,1)]);
t = arrayfun(@(i1)sum(ismember(B(ii(i1,2),:),A(ii(i1,1),:))),(1:size(ii,1))') < 2;
C = A(accumarray(ii(:,1),t) == 4,:);

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

### Community Treasure Hunt

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

Start Hunting!

Translated by