Remove variables not shared by 4 tables

2 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

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

Più risposte (0)

Categorie

Scopri di più su Tables in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by