Common Elements in Two 2D Arrays

Hello Dear All
A = [ 1 2 3 4 5 6;
3 1 2 4 5 7];
B = [ 1 1 3 5 6 7;
3 1 2 4 5 7];
I need Common (A, B) as
C = [1 3;
3 2]
Please Help!

답변 (1개)

Stephen23
Stephen23 2023년 8월 26일
편집: Stephen23 2023년 8월 26일

1 개 추천

A = [1,2,3,4,5,6; 3,1,2,4,5,7]
A = 2×6
1 2 3 4 5 6 3 1 2 4 5 7
B = [1,1,3,5,6,7; 3,1,2,4,5,7]
B = 2×6
1 1 3 5 6 7 3 1 2 4 5 7
Method one: ALL and indexing:
X = all(A==B,1);
C = A(:,X)
C = 2×2
1 3 3 2
Method two: INTERSECT:
C = intersect(A.',B.','rows').'
C = 2×2
1 3 3 2

댓글 수: 2

Muhammad Adil
Muhammad Adil 2023년 8월 26일
Thank you very much sir
Stephen23
Stephen23 2023년 8월 26일
@Muhammad Adil: my pleasure. Please remember to click the accept button.

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

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

질문:

2023년 8월 26일

댓글:

2023년 8월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by