Aggregate functions on grouped data
11 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Muneer
el 25 de Feb. de 2014
Comentada: Akhila
el 20 de Sept. de 2022
Hello, I have three columns of data like this
20 15 111
3 12 111
3 8 111
3 8 111
4 7 166
7 5 166
5 9 166
8 2 166
9 4 166
2 1 200
5 0 200
7 3 200
8 6 200
5 4 200
8 8 200
0 9 250
1 5 250
4 2 250
5 4 275
7 7 275
5 12 275
14 1 275
28 4 275
36 5 300
85 44 300
5 5 300
2 8 300
The third column has values that repeat (entire column is in ascending order) and I would like to do operations on the first and second column based on the groups specified by the third column.
For example, in the third column, the first 4 values are 111, the next 5 are 166. This means one group is the first four values, and the second group is the next 5 values (denoted by number). So I would like to find the maximum of the first group in the first column and the min of the first group in the second column. My output would look like this:
max min sequence
20 8 111
9 2 166
8 0 200
4 2 250
28 1 275
85 5 300
The whole set is about 11 mil rows that would reduce to a smaller set. The problem is the groups or sequences are not the same number of rows. Any help would be greatly appreciated. Max and min aren't the only operations I'd like to do, but they illustrate the idea.
Thanks for your help.
0 comentarios
Respuesta aceptada
Dishant Arora
el 25 de Feb. de 2014
Editada: Dishant Arora
el 25 de Feb. de 2014
[b, ~, n] = unique(yourArray(:,3) , 'first');
firstColumn = accumarray(n , yourArray(;,1) , size(b) , @(x) max(x));
secondColumn = accumarray(n , yourArray(;,2) , size(b) , @(x) min(x));
outputArray = cat(2 , firstColumn , secondColumn , b);
3 comentarios
Akhila
el 20 de Sept. de 2022
Remove the second input argument to the month function in line 2 of the script. The bar graph will now have month numbers in ascending order on the x-axis.
Más respuestas (1)
Camilo Corredor
el 17 de Ag. de 2015
I'd like group the data in function to variable var1 and associated with this make a average of the var2's values. This would be the result
0 = 10,5 and 1 = 46,28
1 comentario
Walter Roberson
el 17 de Ag. de 2015
If you use a table or (older) dataset array and if you have the Statistics Toolbox, then you could use grpstats
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices 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!