equal method to setxor for tables in matlab without setdiff in matlab

You can write a custom function to perform the operation of setxor without using setdiff. Here's an implementation for tables:

main.m
function C = tableSetxor(A, B)
%TABLESETXOR Returns the elements that are in A or B but not in their intersection.
%   TABLESETXOR(A,B) for tables A and B, returns the values from A and B
%   that do not overlap. A and B must have the same variable names, but
%   can have different number of rows. C is a table that has variables with
%   the same data types as A and B.
%
%   Example:
%       A = table([1;2], {'M';'F'}, [20;35], 'VariableNames', {'ID', 'Gender', 'Age'})
%       B = table([2;3], {'F';'M'}, [30;25], 'VariableNames', {'ID', 'Gender', 'Age'})
%       C = tableSetxor(A, B)

%   Copyright 2021 The MathWorks, Inc.

% Find rows that are in A or B but not in their intersection
idx = ~(ismember(A, B, 'rows') | ismember(B, A, 'rows'));

% Combine unique rows from A and B
C = [A(idx,:); B(idx,:)];
end
817 chars
21 lines

This function first uses the ismember function to find the rows that are common between A and B. Then, it negates the result to find the rows that are not common between A and B. Finally, it combines the unique rows from A and B to create C, which is the set with the elements that are in A or B, but not in their intersection.

related categories

gistlibby LogSnag