Remove variables not shared by 4 tables

조회 수: 2 (최근 30일)
Dave
Dave 2019년 12월 12일
답변: Walter Roberson 2019년 12월 12일
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