Aggregate functions on grouped data
조회 수: 13 (최근 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.
댓글 수: 0
채택된 답변
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
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
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/178092/image.png)
댓글 수: 1
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 Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!