How to find differences of 2 matrices which contain other matrices as elements?

1 次查看(过去 30 天)
o1 = eye(3,3);
o6=[1 -1 0; 1 0 0; 0 0 1];
o3=[0 -1 0;1 -1 0 ;0 0 1];
o2=[-1 0 0 ;0 -1 0; 0 0 1];
o3m=[-1 1 0; -1 0 0 ; 0 0 1];
o6m=[0 1 0; -1 1 0 ; 0 0 1];
omxx=[0 -1 0;-1 0 0; 0 0 1];
omx=[-1 1 0; 0 1 0; 0 0 1];
omy=[1 0 0 ;1 -1 0 ;0 0 1];
omxmx=[0 1 0;1 0 0; 0 0 1];
omx2x=[1 -1 0; 0 -1 0; 0 0 1];
om2xx=[-1 0 0 ; -1 1 0; 0 0 1];
G={o1,o6,o3,o2,o3m,o6m,omxx,omx,omy,omxmx,omx2x,om2xx};
labelsG={"o1","o6","o3","o2","o3m","o6m","omxx","omx","omy","omxmx","omx2x","om2xx"};
H={o1,o2};
labelsH={"o1","o2"};
K=setdiff(G,H);
Here my code. I want to find differences of G and H as J=[o6,o3,o3m,o6m,omxx,omx,omy,omxmx,omx2x,om2xx]
But it's not working

回答(2 个)

Jan
Jan 2020-12-13
编辑:Jan 2020-12-14
function [AA, AI] = CellDiff(A, B)
nA = numel(A);
nB = numel(B);
M = false(1, nA); % Typo fixed, thanks Paul
for iA = 1:nA
for iB = 1:nB
if isequal(A{iA}, B{iB})
M(iA) = true;
break;
end
end
end
AI = find(~M);
AA = A(AI);
end

Jan
Jan 2020-12-15
The main problem is the choice of the data representation. Using an indexed field would allow to call setdiff().
Data.o1 = eye(3,3);
Data.o6 = [1 -1 0; 1 0 0; 0 0 1];
Data.o3 = [0 -1 0;1 -1 0 ;0 0 1];
Data.o2 = [-1 0 0 ;0 -1 0; 0 0 1];
Data.o3m = [-1 1 0; -1 0 0 ; 0 0 1];
Data.o6m = [0 1 0; -1 1 0 ; 0 0 1];
Data.omxx = [0 -1 0;-1 0 0; 0 0 1];
Data.omx = [-1 1 0; 0 1 0; 0 0 1];
Data.omy = [1 0 0 ;1 -1 0 ;0 0 1];
Data.omxmx = [0 1 0;1 0 0; 0 0 1];
Data.omx2x = [1 -1 0; 0 -1 0; 0 0 1];
Data.om2xx = [-1 0 0 ; -1 1 0; 0 0 1];
G = {"o1","o6","o3","o2","o3m","o6m","omxx","omx","omy","omxmx","omx2x","om2xx"};
H = {"o1","o2"};
Now setdiff(G, H) get the difference directly. Afterwards you can use something like Data.(G{1}) to get the corresponding values.

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by