How to calculate average of certain value in a matrix?

4 ビュー (過去 30 日間)
afrya
afrya 2013 年 12 月 5 日
編集済み: Andrei Bobrov 2013 年 12 月 5 日
Hello, My problem is about how to calculate the average of certain values in a large matrix. I know how to calculate the 1st average of the matrix with matlab, but for the other one I tried with a for loop but I didn't manage to solve the problem.
Example:
calculate the average of every 2 values of A
A=
1
2
3
4
5
6
7
8
Av1=1,5 WITH MATLAB Av1=sum(A(1:2;1),1)./2
Av2=3,5
Av3=5,5
Av4=7,5
I did the following loop but it doesn't work :
for i=1:3
for k=2:4
Av=sum(A(i*2+1:2*k,1),1)./2
end
end
Thank you in advance

採用された回答

Andrei Bobrov
Andrei Bobrov 2013 年 12 月 5 日
編集済み: Andrei Bobrov 2013 年 12 月 5 日
Av = mean(A([1:2:end;2:2:end]));
or
Av = sum(A([1:2:end;2:2:end]))/2;
ADD
n = 3;
Av = accumarray(floor((n:numel(A)+n-1)/n)',A(:),[],@mean);
or
n=3;
p = reshape([A(:);nan(mod(-numel(A),n),1)],n,[]);
l = isnan(p);
p(l) = 0;
Av = sum(p)./sum(~l);
if rem(numel(A),n) == 0 , just:
Av = mean(reshape(A,n,[]));
if you have Image Processing Toolbox then
Av = blockproc(A,[1 n],@(x)mean(x.data));
  3 件のコメント
afrya
afrya 2013 年 12 月 5 日
I didn't understand the syntax
Andrei Bobrov
Andrei Bobrov 2013 年 12 月 5 日
see ADD part

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

その他の回答 (1 件)

Omair
Omair 2013 年 12 月 5 日
編集済み: Omair 2013 年 12 月 5 日
Do you mean every 2nd value of A?
mean(A(1:2:end))
  1 件のコメント
afrya
afrya 2013 年 12 月 5 日
I mean,how to calculate every 2 values of A At the end, you'll get 4 averages in the case of the exemple

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by