Finding multiple mean values of matric

16 views (last 30 days)
Robin Strak
Robin Strak on 17 Mar 2020
Edited: Guillaume on 17 Mar 2020
I know this is probably not the hardest question, but I can´t find a proper solution at this moment:
I´ve a matrix M (8000x7) and I want to take one column, e.g. M(:,4) and calculate the mean value of different parts of the column. For example having a mean value of 1:50, then 51:100, 101:150, ... and subsequently storing this new array in a variable.
I know this could be a one-liner and would be very happy if you could help me out.
Thanks!

Answers (2)

Arthur Roué
Arthur Roué on 17 Mar 2020
You can use reshape, then mean function.
M_1 = M(:,4)
M_2 = reshape(M, 50, 160) % 160 = 8000/50
M_3 = mean(M_2,1)

Guillaume
Guillaume on 17 Mar 2020
Edited: Guillaume on 17 Mar 2020
If the height of your matrix is a multiple of 50 elements: Simply reshape the column in rows of 50 elements and take the mean across the rows:
assert(mod(size(M, 1), 50) == 0, 'Height is not a multiple of 50')
meanof50elements = mean(reshape(M(:, 4), 50, []), 1); %reshape in rows of 50 elements then take the mean across the rows
If the matrix height is not a multiple of 50, then you have to pad the column with NaNs first, and tell mean to ignore the NaNs:
meanof50elements = mean(reshape([M(:, 4); nan(mod(-size(M, 1), 50), 1)] , 50, []), 1, 'omitnan'); %pad column with NaN to make height a multiple of 50 elements, then reshape into rows of 50 elements and take mean across rows

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by