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

1 view (last 30 days)
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

Answers (2)

Jan
Jan on 13 Dec 2020
Edited: Jan on 14 Dec 2020
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 on 15 Dec 2020
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.

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!