# Mean value every 60 rows

2 views (last 30 days)

Show older comments

Hello, I got my table, with 180 rows and 2 columns, each column is a variable X1, X2. I want to have the mean of X1 60 rows in 60 rows, the same for X2. This is the result media =

60.1635

60.1523

60.1492

media =

513.0252

513.1533

513.1119

Question 1) How to store the results in a matrix or table 3x2 so I would have something like:

60.1635 513.0252

60.1523 513.1533

60.1492 513.1119

Question 2) For example if i wanted to make the average each 120 rows, what would I need to change? is just the "n" value? because i dont understand what does the "1" do in here

a = reshape(tabela(:,j),[],1);

Code is here:

n = 60

for j=1:2

a = reshape(tabela(:,j),[],1);

media = arrayfun(@(i) mean(tabela(i:i+n-1,j)),1:n:length(a)-n+1)'

end

Thanks for your help!

##### 0 Comments

### Accepted Answer

Birdman
on 22 Feb 2018

You need to do some tricks here. Firstly, your data is 181x2 and since 181 is not dividable by 60, I erased the last row to make it 180x2. Next do the following steps:

Tabela=mat2cell(tabela,60*ones(1,3),2)

Tabela_avg=cellfun(@(x) mean(x),Tabela,'uni',0)

and you will obtain the mean for each 60x2 data. You can reach all of them at once by typing

Tabela_avg{:}

If you want the first 120 rows, then you need to separate that as follows:

Tabela=mat2cell(tabela,[120 60],2)

since the sum of dimensions has to be equal to the initial size.

##### 9 Comments

Stephen23
on 22 Feb 2018

Edited: Stephen23
on 22 Feb 2018

"what does the 'uni' and '0' do?"

These are short for 'UniformOutput' and false.

They tell cellfun to put the function outputs into a cell array and return this, which is useful in situations where the function outputs might not be uniform (i.e. are non-scalar). The default ('UniformOutput' = true) tells cellfun that the function outputs are scalar (so they can be joined together into an array the same size as the input cell arrays). See the cellfun help for more info.

### More Answers (2)

sura Naji
on 25 Oct 2019

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!