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