matthiasm@8: function C = mysymsetdiff(A,B) matthiasm@8: % MYSYMSETDIFF Symmetric set difference of two sets of positive integers (much faster than built-in setdiff) matthiasm@8: % C = mysetdiff(A,B) matthiasm@8: % C = (A\B) union (B\A) = { things that A and B don't have in common } matthiasm@8: matthiasm@8: if isempty(A) matthiasm@8: ma = 0; matthiasm@8: else matthiasm@8: ma = max(A); matthiasm@8: end matthiasm@8: matthiasm@8: if isempty(B) matthiasm@8: mb = 0; matthiasm@8: else matthiasm@8: mb = max(B); matthiasm@8: end matthiasm@8: matthiasm@8: if ma==0 matthiasm@8: C = B; matthiasm@8: elseif mb==0 matthiasm@8: C = A; matthiasm@8: else % both non-empty matthiasm@8: m = max(ma,mb); matthiasm@8: bitsA = sparse(1, m); matthiasm@8: bitsA(A) = 1; matthiasm@8: bitsB = sparse(1, m); matthiasm@8: bitsB(B) = 1; matthiasm@8: C = find(xor(bitsA, bitsB)); matthiasm@8: end