Remove variables not shared by 4 tables

1 view (last 30 days)
Hello. How can I remove the variables not shared by 4 tables? Tables have the same number of rows but different number of variables (columns)
The objective is to get the 4 tables but only w variables that are present in each of the 4 individual tables.
I can only do it by pairs with "intersect"
[pair_a, pair_b, pair_c]=intersect(tab1.Properties.VariableNames, tab2.Properties.VariableNames);
tab1=tab1(:,pair_b);
tab2=tab2(:,pair_c);
But then I have to pit tab3 against tab1 and tab2, and then tab4. For 4 tables not too much hassle but as the number of tables increases it gets messier.

Accepted Answer

Walter Roberson
Walter Roberson on 12 Dec 2019
Create a cell array of the tables. For the moment I will call this tab_cell . Then
shared_vars = tab_cell{1}.Properties.VariableNames;
for K = 2 : numel(tab_cell)
shared_vars = interset(shared_vars, tab_cell{K}.Properties.VariableNames);
end
You do not need to compare each table to each other table: A intersect B intersect C = (A intersect B) intersect C = A intersect (B intersect C) = (A intersect C) intersect B -- intersection is transitive and commutative.

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!