Common Elements in Two 2D Arrays

1 次查看(过去 30 天)
Muhammad Adil
Muhammad Adil 2023-8-26
评论: Stephen23 2023-8-26
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
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

类别

Help CenterFile Exchange 中查找有关 Operators and Elementary Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by