How to find the common elements of 2 cell arrays that contain randperm elements?

我与影子孤独终老i 提交于 2019-12-02 07:15:30

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

You can do it by using two loops and check all off them.

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];

%find the size of matrix
[m1,n1] = size(q1);
[m2] = size(q2,1);

for (ii=1:m1)
    for (jj=1:m2)
        %if segments are equal, it will return 1
        %if sum of same segment = 4 it means they are same
        if ( sum( q1(ii,:) == q2(jj,:) ) == n1)
            ii %result of q1
            jj %result of q2
            break;
        end
    end
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!