I want to find which rows in two matrices that are matching.
If I for example have
A =
1 2
4 5
9 7
1 2
6 5
and
B =
2 5
1 2
9 2
1 2
I would like a result like
index_A=[1 4] index_B=[2 4].
Any idea how to solve this?

 채택된 답변

Roger Stafford
Roger Stafford 2014년 6월 7일

7 개 추천

Use the 2nd and 3rd returned arguments in 'intersect':
[~,index_A,index_B] = intersect(A,B,'rows');

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2014년 6월 6일

3 개 추천

C = intersect(A,B,'rows')
index_A= find(ismember(A,C,'rows'));
index_B= find(ismember(B,C,'rows'));

댓글 수: 1

Johan
Johan 2014년 6월 6일
편집: Johan 2014년 6월 6일
thanks, it's a good start, however I realised that it doesn't solve my 'real'fully, since I need index_A and index_B to be of the same length.
If
A =
1 2
4 5
9 7
1 2
6 5
6 5
and
B =
2 5
1 2
9 2
1 2
6 5
this solution would give
index_A = [1 4 5 6] index_B = [2 4 5]
but I need it to be
index_A = [1 4 5] index_B = [2 4 5]
Do you know how to solve it? I'm sorry that the first example didn't cover this case.

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

카테고리

태그

질문:

2014년 6월 6일

답변:

2014년 6월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by