How to creat row mean for every three columns of a matrix?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hey there, I hope someone can help me. I want to create a matrix of the row means for every three columns of a matrix. Something like that:
A=[1 2 3 4 5 6; 1 2 3 4 5 6; 3 4 5 6 7 8; 9 10 11 12 13 14]
mean_x1= mean(A{:,[1:3]},2);
mean_x2= mean(A{:,[4:6]},2);
But I want the means to be in one matrix:
mean=[2 5; 2 5;4 7; 6.67 13]
and I have a much lager matrix with 173 columns, so I think maybe a loop would be the right thing. I just don't know how to do it at all. I hope someone understands what I am looking for.
Thanks!
0 commentaires
Réponses (1)
Guillaume
le 8 Mai 2018
Don't know where that 6.67 comes from. I assume it should be 10:
mean(permute(reshape(A, size(A, 1), 3, []), [1 3 2]), 3)
Note that if you have a variable called mean then a) don't do that and b) clear it before running the above.
What the above does: reshape the variable into a 3D matrix with 3 columns, each group of 3 column now becomes a page. Permute column and pages, then calculate the mean across the pages.
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements 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!