Dear all,
I'm working with a large amount of data in a project.For each row of my matrix , I calculate the average. Here is an example that help you to understand my problem
y1 y2 y3 y4 y5
x1 2 4 4 6 7 Av1=4,6
x2 1 2 3 4 5 Av2=3
x3 1 2 3 4 5 Av3=3
x4 1 2 4 5 6 Av4=3,6
x5 2 5 6 8 9 Av5=6
x6 4 7 8 4 9 Av6=6,4
x7 2 3 4 8 7 Av7=4,8
x8 1 2 4 6 4 Av8=3,4
I want to make my programm user friendly, that means the user enter the number of average that he wants.For instance, if the user enter the number 2 , the programm calculates the average of each 2 rows
y1 y2 y3 y4 y5
x1 2 4 4 6 7 NewAv1=Av1+Av2=3,8
x2 1 2 3 4 5
x3 1 2 3 4 5 NewAv2=Av3+Av4=3,3
x4 1 2 4 5 6
x5 2 5 6 8 9 NewAv3=Av5+Av6=6,2
x6 4 7 8 4 9
x7 2 3 4 8 7 NewAv4=Av7+Av8=4,1
x8 1 2 4 6 4
Thank you in advance

 採用された回答

Andrei Bobrov
Andrei Bobrov 2013 年 12 月 3 日
編集済み: Andrei Bobrov 2013 年 12 月 3 日

1 投票

xy = [2 4 4 6 7
1 2 3 4 5
1 2 3 4 5
1 2 4 5 6
2 5 6 8 9
4 7 8 4 9
2 3 4 8 7
1 2 4 6 4];
k = 2;
i0 = zeros(size(xy,1),1);
i0(1:k:end) = 1;
out = accumarray(repmat(cumsum(i0),size(xy,2),1),xy(:),[],@mean);
ADD
xy2 = xy(1:floor(size(xy,1)/k)*k,:);
s = size(xy2);
i0 = zeros(s(1),1);
i0(1:k:end) = 1;
out = accumarray(repmat(cumsum(i0),s(2),1),xy2(:),[],@mean);

3 件のコメント

afrya
afrya 2013 年 12 月 3 日
Thank you for your answer.I have another problem. For exemple if k=3, I will get Av1= 3.5333 Av2= 5.3333 Av3= 4.1000
How can I delete Av3 because it calculates only the average of the 2 last raws and not the 3 last rows. How can I do it in general for the programm ?
Andrei Bobrov
Andrei Bobrov 2013 年 12 月 3 日
see ADD
Jos (10584)
Jos (10584) 2013 年 12 月 3 日
My suggestion seems more efficient (no cumsum, for instance) ...

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

その他の回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 12 月 2 日

1 投票

%Example--------------
A=rand(10)
p=3
%-----------------------
[n,m]=size(A);
q=ceil(n/p)*p
B=nan(q,m)
B(1:n,:)=A
ii2=p:p:q
ii1=[1 ii2(1:end-1)+1]
out=arrayfun(@(x) nanmean(nanmean(B(ii1(x):ii2(x),:))),1:numel(ii2))'

4 件のコメント

afrya
afrya 2013 年 12 月 2 日
Thanks for your answer, I tried your example, however if you work with a matrix that contains a lot of values, it's not convenient
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 12 月 2 日
What is the size of your matrix?
afrya
afrya 2013 年 12 月 2 日
the size of the matrix is: 3500*579
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 12 月 2 日
編集済み: Azzi Abdelmalek 2013 年 12 月 2 日
%Example--------------
A=rand(3500,379);
p=3;
%-----------------------
tic
[n,m]=size(A);
q=ceil(n/p)*p;
B=nan(q,m);
B(1:n,:)=A;
B=reshape(B',p*m,[]);
out=nanmean(B);
toc
result
Elapsed time is 0.043406 sec

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

Jos (10584)
Jos (10584) 2013 年 12 月 2 日

1 投票

Here is a trick, using clever indexing with accumarray:
% data
A = rand(10,5) ;
K = 3 ; % average of so many rows
% engine
ix = repmat(floor((0:(size(A,1)-1))/K),size(A,2),1).' + 1 % indices
AV = accumarray(ix(:), A(:), [ix(end) 1], @mean) % averages
% check
abs(AV(1) - mean(reshape(A(1:K,:),1,[]))) < eps

3 件のコメント

afrya
afrya 2013 年 12 月 2 日
Thank you for your answer, .. I just have a question about your program, what does eps means?
Jos (10584)
Jos (10584) 2013 年 12 月 3 日
see
doc eps
In many cases, like here, differences smaller than the value of eps are not important
afrya
afrya 2013 年 12 月 3 日
Thanks

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

カテゴリ

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

タグ

質問済み:

2013 年 12 月 2 日

コメント済み:

2013 年 12 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by