Daniel@0: function C = mysetdiff(A,B) Daniel@0: % MYSETDIFF 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 = { things in A that are not in B } Daniel@0: % Daniel@0: % Original by Kevin Murphy, modified by Leon Peshkin Daniel@0: Daniel@0: if isempty(A) Daniel@0: C = []; Daniel@0: return; Daniel@0: elseif isempty(B) Daniel@0: C = A; Daniel@0: return; Daniel@0: else % both non-empty Daniel@0: bits = zeros(1, max(max(A), max(B))); Daniel@0: bits(A) = 1; Daniel@0: bits(B) = 0; Daniel@0: C = A(logical(bits(A))); Daniel@0: end