필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
Ege Tunç
Ege Tunç 2020년 12월 13일
답변: Jan 2020년 12월 15일
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에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by