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!

 採用された回答

Birdman
Birdman 2018 年 2 月 22 日

0 投票

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 件のコメント

Tiago Dias
Tiago Dias 2018 年 2 月 22 日
Oh yeah sorry, 181 my bad.
Could you explain to me what each line does, so i can see the difference from mine.
I asked my Question2 wrongly sorry, what to change in my code or in yours, in the case for me to have each 10 rows a mean, so in that case i would end up with 18 values of mean for each variable
Birdman
Birdman 2018 年 2 月 22 日
Tabela=mat2cell(tabela,60*ones(1,3),2)
Line above separates your initial matrix into 3 matrices having sizes of 60x2 and they are stored in cells.
Tabela_avg=cellfun(@(x) mean(x),Tabela,'uni',0)
This line automatically calculates the mean values for your 3 60x2 sized matrices. You can see them by typing:
Tabela_avg{1}
Tabela_avg{2}
Tabela_avg{3}
Tiago Dias
Tiago Dias 2018 年 2 月 22 日
I express myself wrong, i want a double 3x2 display, so if i want the 1st mean of X1 i call Tabela_avg(1,1) and it comes = 60.1635
Birdman
Birdman 2018 年 2 月 22 日
Then type,
Tabela_avg=cell2mat(cellfun(@(x) mean(x),Tabela,'uni',0))
Tiago Dias
Tiago Dias 2018 年 2 月 22 日
oh yeah dumb me, 3 is the number os blocks created, thats good because i can calculated them previously so instead of 3 i can put a letter or something. for example if it was a mean for each 10 rows would it be:
Tabela=mat2cell(tabela,10*ones(1,18),2)
can it be in doubles? so each mean goes to i =1 , 2 and 3 for j=1...and for j=2 the same?
Birdman
Birdman 2018 年 2 月 22 日
This way it will be double matrices stored in a cell. You can extract them by folloing notation:
Tabela{1}
.
.
Tiago Dias
Tiago Dias 2018 年 2 月 22 日
This does what i want, so thanks for your time and patience!!
A = cell2mat(Tabela_avg)
Tiago Dias
Tiago Dias 2018 年 2 月 22 日
Sorry to bother again, but what does the 'uni' and '0' do?
Stephen23
Stephen23 2018 年 2 月 22 日
編集済み: Stephen23 2018 年 2 月 22 日
"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.

サインインしてコメントする。

その他の回答 (2 件)

KL
KL 2018 年 2 月 22 日
編集済み: KL 2018 年 2 月 22 日

0 投票

Simpler:
No need to use loops/arrayfun or cellfun at all. Simpler solution is to use just reshape with mean,
cc = reshape(mean(reshape(dummy,n,[])),[],2)
cc =
60.1635 513.0252
60.1523 513.1533
60.1492 513.1119

5 件のコメント

Tiago Dias
Tiago Dias 2018 年 2 月 22 日
I express myself wrong, i want a double 3x2 display, so if i want the 1st mean of X1 i call Tabela_avg(1,1) and it comes = 60.1635
KL
KL 2018 年 2 月 22 日
The answer shown above is 3x2 matrix of double!
Tiago Dias
Tiago Dias 2018 年 2 月 23 日
in what u wrote I only see
cc = reshape(mean(reshape(dummy,n,[])),[],2)
so what is dummy?
KL
KL 2018 年 2 月 23 日
That's an example Tiago! You should replace dummy with your matrix.
Tiago Dias
Tiago Dias 2018 年 2 月 23 日
OK, thanks !

サインインしてコメントする。

sura Naji
sura Naji 2019 年 10 月 25 日

0 投票

l have amatrix contact from 1 colum and 1000 rows l want to find the mean and the standard deviation also l want to find the worst value of this because l use the particle swarm optimization

カテゴリ

ヘルプ センター および File ExchangeMatrices and Arrays についてさらに検索

タグ

質問済み:

2018 年 2 月 22 日

回答済み:

2019 年 10 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by