www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/IntersectionofTwoTablesandTheirIndicesExample.m
%% Intersection of Two Tables and Their Indices %% % Create a table, |A|, of gender, age, and height for five people. A = table(categorical({'M';'M';'F';'M';'F'}),... [27;52;31;46;35],[74;68;64;61;64],... 'VariableNames',{'Gender' 'Age' 'Height'},... 'RowNames',{'Ted' 'Fred' 'Betty' 'Bob' 'Judy'}) %% % Create a table, |B|, with rows in common with |A|. B = table(categorical({'F';'M';'F';'F'}),... [31;47;35;23],[64;68;62;58],... 'VariableNames',{'Gender' 'Age' 'Height'},... 'RowNames',{'Meg' 'Joe' 'Beth' 'Amy'}) %% % Find the rows common to both |A| and |B|, as well as the index vectors % |ia| and |ib|, such that |C = A(ia,:)| and |C = B(ib,:)|. [C,ia,ib] = intersect(A,B) %% % Two rows that have the same values, but different names, are considered % equal. Therefore, we discover that Betty, |A(3,:)|, and Meg, |B(1,:)| % have the same gender, age, and height.