Remove variables not shared by 4 tables

1 次查看(过去 30 天)
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.

采纳的回答

Walter Roberson
Walter Roberson 2019-12-12
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by