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

2 views (last 30 days)
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

Accepted Answer

Voss
Voss on 27 Jan 2022
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

More Answers (0)

Categories

Find more on Time Series in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!