Aggregate functions on grouped data

19 vues (au cours des 30 derniers jours)
Muneer
Muneer le 25 Fév 2014
Commenté : Akhila le 20 Sep 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.

Réponse acceptée

Dishant Arora
Dishant Arora le 25 Fév 2014
Modifié(e) : Dishant Arora le 25 Fév 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 commentaires
Muneer
Muneer le 28 Fév 2014
Can this be used on data that isn't a cell array of strings? In a previous step, I do this process
segarray = textscan(fid, FormatString, block_size, 'delimiter',char(9));
for i = 1 : 5(segarray);
segarray{i} = str2double(segarray{i}) ;
isn = isnan(segarray{i}) ;
segarray{i}(isn) = 0 ;
end
In order to find "NULL" values and replace them with zero. However, I think this is why I am having an error with the function you suggested (unique).
Akhila
Akhila le 20 Sep 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.

Connectez-vous pour commenter.

Plus de réponses (1)

Camilo  Corredor
Camilo Corredor le 17 Août 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 commentaire
Walter Roberson
Walter Roberson le 17 Août 2015
If you use a table or (older) dataset array and if you have the Statistics Toolbox, then you could use grpstats

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by