This problem might be solved more easily and efficiently, but this solution should get you through your example.
Walter's concerns are valid, and it may just take your seeing an example without a solution, which you thought for sure had one, to see what he means.
G = [1 2 3 4 5 6; 4 5 7 4 5 6; 1 2 7 1 2 3; 4 5 7 1 2 7]
L = size(G,1);
for ii = 2:L
T = ismember(G(ii:L,1:3),G(ii-1,4:6),'rows');
T2 = ismember(G(ii:L,4:6),G(ii-1,4:6),'rows');
if any(T)
idx = find(T)+ii-1;
tmp = G(idx,:);
G(idx,:) = G(ii,:);
G(ii,:) = tmp;
elseif any(T2)
idx = find(T2)+ii-1;
tmp = G(idx,:);
G(idx,:) = G(ii,:);
G(ii,:) = tmp;
G(ii,4:6) = G(ii,1:3);
G(ii,1:3) = G(ii-1,4:6);
else
error('No solution')
end
end
G
Best Answer