sum special rows on a matrix with unique function
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hey guys!
what I want is to sum column 5 just if the values from row A(:,1:4) are equals as in the e.g
A = [9 1001381 18 2014 2;
9 1002741 18 2014 45;
17 1002452 18 2014 250;
21 1003292 18 2014 225; %this row is the same as the following one.
21 1003292 18 2014 75;
20 1002367 18 2014 24;
27 1001579 18 2014 20;
27 1001802 18 2014 15];
sumvtas = grpstats(A(:,5),A(:,1:4),{@sum})
[sumvtas,count]=grpstats(A(:,5),A(:,1:4),{@sum,@numel})
results=matrix(num2str(unique(A(:,1:4))),sumvtas,count, ...
'VariableNames',{'PEx','Mat','Week','Year','sum','count'})
This should be the solution:
Results=
[9 1001381 18 2014 2;
9 1002741 18 2014 45;
17 1002452 18 2014 250;
21 1003292 18 2014 300; % here's the sum it should give.
20 1002367 18 2014 24;
27 1001579 18 2014 20;
27 1001802 18 2014 15];
Thanks!
0 comentarios
Respuestas (2)
Walter Roberson
el 1 de Jun. de 2017
A = [9 1001381 18 2014 2;
9 1002741 18 2014 45;
17 1002452 18 2014 250;
21 1003292 18 2014 225; %this row is the same as the following one.
21 1003292 18 2014 75;
20 1002367 18 2014 24;
27 1001579 18 2014 20;
27 1001802 18 2014 15];
[urows, ~, group] = unique(A(:,1:4), 'rows', 'stable');
[sumvtas, count] = grpstats( A(:,5), group, {@sum,@numel});
results = array2table( [urows, sumvtas, count], ...
'VariableNames',{'PEx','Mat','Week','Year','sum','count'});
0 comentarios
Ver también
Categorías
Más información sobre Elementary Math en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!