Find the intersect of two columns from different matrix but keep the corresponding cells from the same row along with the intercept values

조회 수: 4 (최근 30일)
I have two matrix with two columns. For example
A = [2 2; 4 3; 6 5; 8 3; 10 2]
B = [2 1; 3 2; 5 2; 8 2; 10 1]
I want to find the values from the first column that intersect. I can do this using 'C = intersect(A(:,1),B(:,1)). However, the output that I actaully want is a new matrix which keeps the corresponding values from the 2nd column of matrix A and B, along with the intercept values not just the intercept values which are spat out from the intersect function. So I end up with:
D = [2 8 10;
2 3 2;
1 2 1]
The above is a simplified example but I want to be able to apply it to a dataset with thousands of rows.

채택된 답변

Stephen23
Stephen23 2019년 10월 15일
편집: Stephen23 2019년 10월 15일
>> A = [2,4,6,8,10;2,3,5,3,2]
A =
2 4 6 8 10
2 3 5 3 2
>> B = [2,3,5,8,10;1,2,2,2,1]
B =
2 3 5 8 10
1 2 2 2 1
>> [V,X,Y] = intersect(A(1,:),B(1,:));
>> D = [V;A(2,X);B(2,Y)]
D =
2 8 10
2 3 2
1 2 1
  댓글 수: 3
Stephen23
Stephen23 2019년 10월 16일
>> A = [2,2;4,3;6,5;8,3;10,2]
A =
2 2
4 3
6 5
8 3
10 2
>> B = [2,1;3,2;5,2;8,2;10,1]
B =
2 1
3 2
5 2
8 2
10 1
>> [V,X,Y] = intersect(A(:,1),B(:,1));
>> D = [V,A(X,2),B(Y,2)]
D =
2 2 1
8 3 2
10 2 1

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by