Extracting the mean of 2nd column based on unique 1st column values.

조회 수: 1 (최근 30일)
Rahul Gulia
Rahul Gulia 2022년 1월 26일
댓글: Rahul Gulia 2022년 1월 27일
I have this vector,
1 22.6391
1 22.6357
1 22.6338
1.5 22.1159
1.5 22.1056
1.5 22.0950
1.5 22.08820
2 22.11870
2 22.11100
2 22.10280
2 22.09650
And I want a mean of the 2nd column values based on the unique 1st column values. Like,
1 22.63
1.5 22.10
2 22.10
I have tried using tmp = [unique( SINR_tmp(:,1) ),accumarray( SINR_tmp(:,1), SINR_tmp(:,2), [], @mean )];
but this gives unique values only when 1st column has integer values. But I need to include the floating values too in the 1st column.
I have also tried using the function unique() in various ways. But could not succeed in getting the desired result.
I would really appreciate any kind of help on this.
Thank you,
Rahul Singh Gulia

채택된 답변

Voss
Voss 2022년 1월 27일
Here are two similar methods that produce identical results:
SINR_tmp = [ ...
1 22.6391; ...
1 22.6357; ...
1 22.6338; ...
1.5 22.1159; ...
1.5 22.1056; ...
1.5 22.0950; ...
1.5 22.08820; ...
2 22.11870; ...
2 22.11100; ...
2 22.10280; ...
2 22.09650; ...
];
% Method 1:
uS = unique(SINR_tmp(:,1));
uS(:,2) = NaN;
for ii = 1:size(uS,1)
uS(ii,2) = mean(SINR_tmp(SINR_tmp(:,1) == uS(ii),2));
end
disp(uS);
1.0000 22.6362 1.5000 22.1012 2.0000 22.1073
% Method 2:
[uS,~,jj] = unique(SINR_tmp(:,1));
uS(:,2) = NaN;
for ii = 1:size(uS,1)
uS(ii,2) = mean(SINR_tmp(jj == ii,2));
end
disp(uS);
1.0000 22.6362 1.5000 22.1012 2.0000 22.1073

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Time Series에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by