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?
For example:
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]}
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...
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