Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

Joining 2 tables with outer join both having extra elements

1 Ansicht (letzte 30 Tage)
Mehul Agrawal
Mehul Agrawal am 27 Jun. 2016
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
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 Kommentar
Ruchir Kemnaik
Ruchir Kemnaik am 6 Jul. 2016
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;

Antworten (0)

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by