필터 지우기
필터 지우기

Join 3 tables based on 2 key fields, trouble using outerjoin()

조회 수: 1 (최근 30일)
newbie9
newbie9 2020년 4월 6일
댓글: newbie9 2020년 4월 6일
How can I combine 3 tables into 1 table, using two "key" fields? I have tried various combinations of outerjoin() without any luck. Thanks in advance for any help, I'm sure I'm missing something obvious. Using Matlab R2018a, no fancy add-ons.
Code
%%% set up dummy data tables
Key1 = [1 1 1 2 2 3 3 3 3 3];
Key2 = [1 2 3 1 2 1 2 3 4 5];
Val1 = [0 NaN NaN 0 NaN 0.09 NaN NaN NaN NaN];
Val2 = [NaN 0.55 0.55 0.04 0.04 0.58 0.634 0.668 0.6950 0.7560];
mytable = array2table([Key1', Key2', Val1', Val2']);
mytable.Properties.VariableNames = {'Key1', 'Key2', 'Val1', 'Val2'};
temp1 = array2table([1 4 0; 2 3 0; 3 6 0.09]);
temp1.Properties.VariableNames = {'Key1', 'Key2', 'Val1'};
temp2 = array2table([1 4 0.55; 2 3 0.04; 3 6 0.07560]);
temp2.Properties.VariableNames = {'Key1', 'Key2', 'Val2'};
%%% try to join mytable, temp1, and temp2 together
Tout = outerjoin(mytable, temp1, 'MergeKeys', true);
Tout = outerjoin(Tout, temp2, 'MergeKeys', true);
Current Result
I want the highlighted rows to be combined, such that the Key1-Key2 pair is not duplicated in the output table.
Example Desired Result
  댓글 수: 1
newbie9
newbie9 2020년 4월 6일
Got it-- I just needed to switch the order:
Tout = outerjoin(temp1, temp2, 'MergeKeys',true);
Tout = outerjoin(mytable, Tout, 'MergeKeys',true);

댓글을 달려면 로그인하십시오.

답변 (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