How to effectively get the mean for every three consecutive outputs?
Show older comments
Hello, I have an array that is (21,7). I want to get the mean of every three rows from start to finish. For example, my results would output my dependent variable as [0 0 0 1 1 1 2 2 2] while my results/independent variable would be [2 3 2 9 8 7 17 21 16]. I would like to get a result where I get [0 1 2] and with the means [2.333 8 18]. Anyone have a code that would work for all of the columns in my array. I'm novice at MATLAB, so an easy to understand code would really help. If easier or more useful, if I could do the mean of the results for every dependent variable that was the same, that would work too (so I can avoid sorting next time). Thanks!
Accepted Answer
More Answers (1)
Bruno Luong
on 12 Oct 2018
If you want to avoid reshape, nd arrays, etc...
C = [zeros(1,size(A,2)); cumsum(A,1)];
M3 = diff(C(1:3:end,:),1,1)/3
That code will ignore the last trailing part in case length(A) is not divisible by 3.
Categories
Find more on Creating and Concatenating Matrices 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!