如何找到包含randperm元素的2个单元格数组的公共元素? [英] How to find the common elements of 2 cell arrays that contain randperm elements?

查看:57
本文介绍了如何找到包含randperm元素的2个单元格数组的公共元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个单元格数组,它们的大小可能不相同.单元格数组的元素是整数的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆