如何找到包含randperm元素的2个单元格数组的公共元素? [英] How to find the common elements of 2 cell arrays that contain randperm elements?
问题描述
我有两个单元格数组,它们的大小可能不相同.单元格数组的元素是整数的randperm
. randperm
数据类型为双精度数组.如何找到两个单元格阵列的共同元素?
I have two cell arrays which may not be of the same size. Elements of cell arrays is randperm
of an integer number. randperm
data Type is double array. How can I find common elements of two cell arrays?
例如:
Q1 = {[1 2 3 4], [3 2 4 1], [4 2 1 3]}
Q2 = {[2 4 3 1], [1 2 3 4], [1 2 4 3]}
正如我所说的,单元格数组的元素是randperm
.我希望上面示例的输出是"Q1
的元素-1,即[1 2 3 4]
,因为它也出现在Q2
中.
注意:单元阵列的列数可能不同...
As I said elements of cell arrays are randperm
. I want the output of above example be "Element-1 of Q1
i.e. [1 2 3 4]
since it is also present in Q2
.
Note: Cell Arrays may have different number of columns...
推荐答案
垂直地连接单元格阵列中的矩阵,并将intersect
与
Vertically concatenate the matrices inside the cell arrays and use intersect
with the 'rows'
flag. i.e.
Q1={[1 2 3 4], [3 2 4 1], [4 2 1 3]};
Q2={[2 4 3 1], [1 2 3 4], [1 2 4 3]};
Qout = intersect(vertcat(Q1{:}), vertcat(Q2{:}), 'rows');
%>> Qout
%Qout =
% 1 2 3 4
这篇关于如何找到包含randperm元素的2个单元格数组的公共元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!