Finding multiple mean values of matric

9 vues (au cours des 30 derniers jours)
Robin Strak le 17 Mar 2020
Modifié(e) : Guillaume le 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!
0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

Réponses (2)

Arthur Roué le 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)
0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

Guillaume le 17 Mar 2020
Modifié(e) : Guillaume le 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
0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by