Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

Joining 2 tables with outer join both having extra elements

3 vues (au cours des 30 derniers jours)
Mehul Agrawal
Mehul Agrawal le 27 Juin 2016
Clôturé : MATLAB Answer Bot le 20 Août 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 commentaire
Ruchir Kemnaik
Ruchir Kemnaik le 6 Juil 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;

Réponses (0)

Cette question est clôturée.

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by