Aggregate functions on grouped data
19 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposta accettata
Dishant Arora
il 25 Feb 2014
Modificato: Dishant Arora
il 25 Feb 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 Commenti
Akhila
il 20 Set 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.
Più risposte (1)
Camilo Corredor
il 17 Ago 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 Commento
Walter Roberson
il 17 Ago 2015
If you use a table or (older) dataset array and if you have the Statistics Toolbox, then you could use grpstats
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!