How to mean data of second column with respect to the similar element in the first column

2 views (last 30 days)
Dear All,
I have a data array with two column. The first array is contain repeated values of different order and the second column contain the respective data. I need to mean elements in second column which has same value in the first column. Example of data pic attached hereby. According to the figure i need to mean only values of column two correspond to 0, 0.016667 and so on. Thank you in advance

Answers (4)

Cris LaPierre
Cris LaPierre on 14 Nov 2022
Edited: Cris LaPierre on 14 Nov 2022
Use groupsummary or grpstats. Group on column A, and set the 'method' to 'mean', and set the datavars to column B.
If you import you data to a table, use this syntax:
If your data is in vectors, use this syntax

Paul on 14 Nov 2022
Check out splitapply. Applies directly to this problem.

VBBV on 15 Nov 2022
Edited: VBBV on 15 Nov 2022
This is one approach
D(:,1) = [zeros(6,1); ones(6,1)*0.01667; ones(6,1)*0.05];
D(:,2) = rand(length(D),1);
o = unique(D(:,1));
for k = 1:length(o)
idx = find(D(:,1) == o(k));
Data(k) = mean(D(idx,2));
Data = 1×3
0.4640 0.4352 0.4520

VISHNU on 15 Nov 2022
Dear guys, Thank you for your Answers.
I tried like the following below and it worked well.
[C,ia] = unique(data(:,1));
for n=1:length(ia)-1


Find more on Tables 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!

Translated by