MATLAB Answers

How can i compute the mean of specific number of table rows?

4 views (last 30 days)
Ioannis Tsikriteas
Ioannis Tsikriteas on 9 Aug 2017
Commented: Cong Ba on 10 Aug 2017
Hi, I have a large amount of table data which is divided by date and be more specific i have data that is per 10 minutes and i want to compute the mean of them per hour! In other words, i want ot compute the mean of my data per 10 rows until my data is finished! Is this possible and if yes....which command should i use?


Sign in to comment.

Accepted Answer

Cong Ba
Cong Ba on 9 Aug 2017
Try reshape:
a = randn(100,1); % assume this is the column you have
b = reshape(a,[10,10]); % reshape it so each column has 10 rows
avg = mean(b);


Show 1 older comment
Cong Ba
Cong Ba on 9 Aug 2017
let's assume you throw the first 2 rows...
a = randn(278984,2); % assume this is your table
a = a(3:end,:); % assme throw out the first two rows
b1 = reshape(a(:,1),[6, 278982/6]); % reshape the first column to [6, 278982/6] so it has 6 rows
avg1 = mean(b1);
b2 = reshape(a(:,2),[6, 278982/6]); % same to the second column
avg2 = mean(b2);
avg1 would be the hourly series you want for the first column of your data. avg2 would be for the second.
Ioannis Tsikriteas
Ioannis Tsikriteas on 10 Aug 2017
When i try the reshape command i get the following message: Error using tabular/reshape (line 150) Undefined function 'reshape' for input arguments of type 'table'.
To be more clear isend a pic of the shape of my data tables
Cong Ba
Cong Ba on 10 Aug 2017
Andrei is using the timetable function which may better suit your need (you essentially have a time series). But if you want to manipulate data in matrix form (my code works for matrix form), you can try this function: table2array

Sign in to comment.

More Answers (2)

Andrei Bobrov
Andrei Bobrov on 9 Aug 2017
n = 278984;
t = minutes(10*(1:n)');
D = randi(255,n,2); % Let D - your data (278984 x 2)
TT = timetable(t,D(:,1),D(:,2));
TT_out = retime(TT,'hourly','mean');


Ioannis Tsikriteas
Ioannis Tsikriteas on 10 Aug 2017
I am sorry but i didn't understand the concept of these commands.
Mine data are already ona a table 278984x2 and has the following shape:
I am searching a way to compute the mean between the fifth and the eleventh row for example in order to have my data per hour, not per 10 minutes
Andrei Bobrov
Andrei Bobrov on 10 Aug 2017
a.x_10_double = [a.x_10_{:}]';
TT = table2timetable(a(:,[1,3]),'RowTimes','x08_Jul_100_10_00');
TT_out = retime(TT,'hourly','mean');
a2 = table(a{:,1},[a{:,2}{:}]','v',{'datetime','x_10_double'})
a2.groups = hour(a2{:,1});
a_out = varfun(@mean,a2,'Group','groups');

Sign in to comment.

Peter Perkins
Peter Perkins on 10 Aug 2017
Add a grouping variable to your table and use varfun. Something like
n = ceil(height(t)/10);
g = repelem(1:n,10)';
t.Group = g(1:height(t));
t.groupMeans = varfun(@mean,t,'GroupingVariable','Group')


Sign in to comment.


Community Treasure Hunt

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

Start Hunting!

Translated by