Info

此问题已关闭。 请重新打开它进行编辑或回答。

Joining 2 tables with outer join both having extra elements

1 次查看(过去 30 天)
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