Merging the content of two tables
5 views (last 30 days)
Show older comments
I have two tables Ta and Tb that have different number of rows and different number of columns. The content is all cell text, but maybe in the future it could also contain cell number.
.
I want to merge the content of these tables together under the following set of rules:
- Take the value of Ta(i,j) if Tb(i*,j*) is empty and vice versa.
- If both are available, then take the value of Ta(i,j) (or optionally, check whether they are the same).
.
The tricky part however is that we do not have unique row keys, we only have unique column keys. Note above that I make a distinction between i* and i. The reason is that the row in Ta can be at a different index than Tb, same holds for the columns j* and j. The implications are:
- that we first need to identify which row for Ta corresponds to the row of Tb and vice versa. We can do this by trying to crossmatch any of the columns that the tables share in common.
To make it also harder:
- the number of rows are not equal for the tables (sometimes there are rows in Ta but not in Tb and vice versa).
.
How can we merge the content of these two tables together in the most efficient way?
.
Example variables to play with:
Ta = cell2table({...
'a1', 'b1', 'c1'; ...
'a2', 'b2', 'c2'}, ...
'VariableNames', {'A','B', 'C'})
Tb = cell2table({...
'b2*', 'c2', 'd2'; ...
'b3', 'c3', 'd3'; ...
'b4', 'c4', 'd4'}, ...
'VariableNames', {'B','C', 'D'})
The resulting table Tc should be something like this:
Tc = cell2table({...
'a1' 'b1' 'c1' ''; ...
'a2' 'b2' 'c2' 'd2'; ...
'' 'b3' 'c3' 'd3'; ...
'' 'b4' 'c4' 'd4'}, ...
'VariableNames', {'A', 'B','C', 'D'})
6 Comments
Guillaume
on 19 Oct 2017
My understanding is that you get a match if any key in the keyset match, not just all the keys (or all the non-null keys which would be more manageable). When one key matches but not the other you take any of the value as the value for the result.
Answers (1)
KSSV
on 18 Oct 2017
Tc = outerjoin(Ta,Tb,'MergeKeys', 1)
6 Comments
Life is Wonderful
on 14 May 2020
Edited: Life is Wonderful
on 15 May 2020
Suggestion :
Original Requirement was
A B C D
__________ ______ ______ __________
{'a1' } {'b1'} {'c1'} {0×0 char}
{'a2' } {'b2'} {'c2'} {'d2' }
{0×0 char} {'b3'} {'c3'} {'d3' }
{0×0 char} {'b4'} {'c4'} {'d4' }
Actual Output of
Tbig =
4×4 table
A B C D
__________ ______ ______ __________
{'a1' } {'b1'} {'c1'} {0×0 char}
{0×0 char} {'b3'} {'c3'} {'d3' }
{0×0 char} {'b4'} {'c4'} {'d4' }
{'a2' } {'b2'} {'c2'} {'d2' }
The incorrect/Bug outcome is from
Tbig = [Tbig; mergedRow]; %#ok<AGROW>
Suggestion and proposal is
Tbig = outerjoin(Tbig,mergedRow,'MergeKeys', true);
You get the desired outcome
A B C D
__________ ______ ______ __________
{'a1' } {'b1'} {'c1'} {0×0 char}
{'a2' } {'b2'} {'c2'} {'d2' }
{0×0 char} {'b3'} {'c3'} {'d3' }
{0×0 char} {'b4'} {'c4'} {'d4' }
See Also
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!