How can I find average of a range of values of the first column in a two column matrix
조회 수: 15 (최근 30일)
이전 댓글 표시
I have a two column matrix, with the first column having distances r and the second column, the value of a physical quantity measured at that r. The r values are not equally spaced, though they are in the ascending order. I need to take the average of the r values that are nearby (say from 1. 2000 to 1.2999) and the corresponding average of the quantity in the second column and generate a new matrix with the average values
r Q r_new Q_new
1.253048 0.939051 1.27 0.9197
1.257249 0.900010 1.3596 0.8023
1.299932 0.920000 1.45338 0.8188
1.305562 0.892354 ............ ............
1.356277 0.785555 ............ ..............
1.386665 0.635889 50.226 0.5684
1.389999 0.895554
1.420385 0.852316
1.486392 0.785361
................ ...............
................ ...............
50.02786 0.567852
50.42389 0.568992
댓글 수: 0
채택된 답변
Cris LaPierre
2021년 10월 5일
편집: Cris LaPierre
2021년 10월 5일
Try using groupsummary or grpstats. In both, you can specify bins to use for grouping data in one variable, and use that grouping to compute the mean value of a second variable of equal size.
Consider this example:
% Sales data in a table
Profit = [2032 3071 1185 2587 1998 2899 3112 909 2619 3085]';
TotalItemsSold = [14 13 8 5 10 16 8 6 7 11]';
sales = table(Profit,TotalItemsSold)
% Compute mean number of items sold by Profit grouped into bins of 1000
avg = groupsummary(sales,"Profit",[0:1000:4000],"mean","TotalItemsSold")
댓글 수: 3
Cris LaPierre
2021년 10월 6일
Rather than duplicating the first column, you could use the 'datavars' input.
avg = groupsummary(G,"r",[1.2:0.1:50.5],"mean",["r","Q"]);
Using the example above:
% Sales data in a table
Profit = [2032 3071 1185 2587 1998 2899 3112 909 2619 3085]';
TotalItemsSold = [14 13 8 5 10 16 8 6 7 11]';
sales = table(Profit,TotalItemsSold);
% Compute mean profit & number of items sold by Profit grouped into bins of 1000
avg = groupsummary(sales,"Profit",[0:1000:4000],"mean",["Profit","TotalItemsSold"])
추가 답변 (1개)
Voss
2021년 10월 5일
If A is your two column matrix, then A_new below is the average values of the rows where the first column of A is greater than or equal to 1.2 and less than 1.3:
idx = A(:,1) >= 1.2 & A(:,1) < 1.3;
A_new = mean(A(idx,:),1);
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!