Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Joining 2 tables with outer join both having extra elements

1 回表示 (過去 30 日間)
Mehul Agrawal
Mehul Agrawal 2016 年 6 月 27 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
I have 2 tables having names and scores of individuals. I want to find the total marks scored by people in the tests. If they took one test, it is that, else it is sum of two (or more) tests. Eg: Data is like:
A.Name = {'S';'P';'R';'M';'K'};
A.Score = [80;82;85;95;55];
>> A = struct2table(A);
B.Name = {'Mi';'J';'K'};
B.Score = [80;82;25];
B = struct2table(B);
I am trying to create a table by joining:
X = join(A, B, 'MergeKeys', true, 'Keys','Name');
X.Score_A(isNan(X.Score_A)) = 0;
X.Score_B(isNan(X.Score_B)) = 0;
X.Score = X.Score_A+X.Score_B;
Is there a more efficient way to do this ?
Sorry, I didn't mention one more step. I had to first do a union to create A, B with all elements. Else, just join fails.
  1 件のコメント
Ruchir Kemnaik
Ruchir Kemnaik 2016 年 7 月 6 日
Hi Mehul,
Did the above code work for you? It will give an error since "MergeKeys" is not a valid parameter for "join" function. I used "outerjoin" function and it gave the expected result. The code I used is as follows:
X = outerjoin(A,B, 'MergeKeys', true, 'Keys','Name');
X.Score_A(isnan(X.Score_A)) = 0;
X.Score_B(isnan(X.Score_B)) = 0;
X.Score = X.Score_A+X.Score_B;

回答 (0 件)

この質問は閉じられています。

製品

Community Treasure Hunt

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

Start Hunting!

Translated by