Extracting the mean of 2nd column based on unique 1st column values.
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
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);
% 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);
More Answers (0)
See Also
Categories
Find more on Fourier Analysis and Filtering 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!