简体   繁体   English

MATLAB 3D数组的交集

[英]MATLAB intersection of 3d arrays

I have two 3d arrays: 我有两个3D阵列:

A(:,:,1) = [1 2 3; 4 5 6; 7 8 9];
A(:,:,2) = [1 0 0; 0 1 0; 0 0 1];
A(:,:,3) = [3 2 1; 6 5 4; 9 8 7];
...

and

B(:,:,1) = [1 1 1; 2 2 2; 3 3 3];
B(:,:,2) = [1 0 0; 0 1 0; 0 0 1];
B(:,:,3) = [3 3 3; 2 2 2; 1 1 1];
...

They both consist of 3x3 matrices and their third dimensions are very large. 它们都由3x3矩阵组成,并且第三个维度非常大。 I want to obtain the array of matrices that exist in both arrays. 我想获取两个数组中都存在的矩阵数组。 I am doing it in a for loop by comparing element-wise (matrix-wise). 我在for循环中通过比较按元素(按矩阵)进行操作。 It takes a very long time, so I am looking for an easier way (or an existing function) to do the same. 这需要很长时间,因此我正在寻找一种更简单的方法(或现有功能)来执行此操作。

Thanks! 谢谢!

Collapse the first two dimensions into one and transpose, so that matrices of the 3D array become rows of a matrix. 将前两个维折叠为一个并进行转置,以便3D数组的矩阵成为矩阵的行。 That way you can use intersect(...,'rows') . 这样,您可以使用intersect(...,'rows') Finally, transpose back and reshape back: 最后,移回并重新调整形状:

[m, n, p] = size(A);
result = intersect(reshape(A, [], p).', reshape(B, [], p).', 'rows');
result = reshape(result.', m, n, []);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM