Cumsum of array, sum of previous data
Afficher commentaires plus anciens
I have a database, where each row is a tennis match, you can read ID_Tournament, ID_Round, DATE_Game, ID1 Player1, ID2 Player2, FirstServe Player1, DoubleFault Player1, FirstServe Player2, DoubleFault Player2. I have imported the file in mathlab and now I want to create new columns next to the last one (column I)

The new column should be the SUM of the previous match statics of that player. I'll give you an example referred to the image. I would like to know how many FS player1 had before in the row11 (ID1 488). So in J11 I'll read 47 (only one match before row11 occurred). Instead in row16 in J16 the player ID1 488 previously had 2 matches, so the correct data should be 47+52=99. In each row I want to read the new data about previous matches. How could it be script? How can I set a parameter that could indicate how many days one should look back?
In matlab answer an user gave me this script that actually works but not properly, however I think it's a good start:
load('demo.mat');
d = demoS1{:,[4,6]};
[a,ii] = sortrows(d,1);
[~,~,c] = unique(a(:,1));
d1 = accumarray(c,a(:,2),[],@(x){cumsum(x(:))});
d1 = cat(1,d1{:});
[~,i1] = sort(ii);
demoS1.last_column = d1(i1);
Above, the script sum previous match and also the match that we are analyzing, and of course it is incorrect. Also I can't set (or I don't know how) how many days (or previous match) the script have to take and put in the sum.
Need your help guys. Thanx in advance
Réponse acceptée
Plus de réponses (1)
Mirko Piccolo
le 11 Sep 2017
Modifié(e) : Mirko Piccolo
le 11 Sep 2017
0 votes
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!