how to take a mean of specific rows

36 次查看(过去 30 天)
pruth
pruth 2017-9-12
评论: KL 2017-9-13
hi. i have created a mat file which contains 42 rows and 7 number of columns 42 rows represent 42 months started from January.(3 years 6 months) i want to take a mean of all monthly values all January(rows 1,13,25,37) mean all feb (rows 2,14,26,38) like that for all months
so it would give me 12 rows and 7 columns.
hope you understand. any help will appreciable.

回答(2 个)

Image Analyst
Image Analyst 2017-9-12
Assuming you have, or can add, a column with the month number, then simply use grpstats(), if you have the Statistics and Machine Learning Toolbox.
monthlyMeans = grpstats(yourTable, monthColumn);

KL
KL 2017-9-12
编辑:KL 2017-9-12
your_variable_mean_1 = mean(your_variable([1 13 25 37],:),1) %row-wise mean
your_variable_mean_2 = mean(your_variable([2 14 26 36],:),1)
your_variable_mean_1 = mean(your_variable([1 13 25 37],:),2) %column-wise mean
your_variable_mean_2 = mean(your_variable([2 14 26 36],:),2)
But this is not the ideal way, you should include month number in a column and create a table. Then you can use month as grouping variable and do your calculations in the proper way.
  2 个评论
pruth
pruth 2017-9-13
hi thanks for replying... how can we use loop for it. I don't want to write a code for each month... hope u understand.
KL
KL 2017-9-13
No need for a loop. Do it the right way and it's very simple. So I've created a month number vector and calculating monthly means for all the columns in a new table. Suit it to your needs!
%unnamed is your array of 42x7.
mon_no = repmat((1:12)',4,1); %here goes your month numbers
T = table(mon_no(1:42), unnamed, 'VariableNames',{'mon_no','values'});
mean_T = varfun(@mean,T,'GroupingVariables','mon_no');

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by