Aggregate functions on grouped data

11 次查看(过去 30 天)
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.

采纳的回答

Dishant Arora
Dishant Arora 2014-2-25
编辑:Dishant Arora 2014-2-25
[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 个评论
Muneer
Muneer 2014-2-28
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 2022-9-20
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.

请先登录,再进行评论。

更多回答(1 个)

Camilo  Corredor
Camilo Corredor 2015-8-17
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 个评论
Walter Roberson
Walter Roberson 2015-8-17
If you use a table or (older) dataset array and if you have the Statistics Toolbox, then you could use grpstats

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by