I want to get the mean of cells in column 4 based on the column 1 value.

1 view (last 30 days)
Leah Davis
Leah Davis on 9 Dec 2021
Commented: Leah Davis on 10 Dec 2021
Column 1 is numbers 259:374 and could be converted to datetime if easier. It contains decimals for the hours and minutes.
I want to create a mean of all the corresponding values in column 4, from to that specific day,(in column 1) eg 259.00 to 259.99 and do this for every day cycling through to 374.
Ive tried creating a loop but i honestly have no idea where to start, any help would be appreciated.

Accepted Answer

David Hill
David Hill on 9 Dec 2021
m=floor(yourMatrix(:,1));
M=259:374;
for k=1:length(M)
n(k)=mean(yourMatrix(m==M(k),4));
end

More Answers (1)

Image Analyst
Image Analyst on 9 Dec 2021
Try splitapply(). It's meant for this. You could probably also use groupsummary() or grpstats().
% Create random data:
column4 = sort(259 + (374-259) * rand(1000, 1))
% Group into groups that have the same value to the left of the decimal point.
groups = findgroups(floor(column4))
% Take the mean of each group.
means = splitapply(@mean, column4, groups)

Community Treasure Hunt

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

Start Hunting!