how to take the average of a matrix of any size

回答 (2 件)

Wayne King
Wayne King 2012 年 3 月 14 日

1 投票

mean()
X = randn(100,100);
mean(mean(X))
or
mean(X(:))

4 件のコメント

lowcalorie
lowcalorie 2012 年 3 月 14 日
im sorry i meant matrix of any size
Sean de Wolski
Sean de Wolski 2012 年 3 月 14 日
X = rand(3,3,3,3,3);
Wayne King
Wayne King 2012 年 3 月 14 日
If you want the means of the columns or the rows, you can use mean() with the dimension argument
X = mean(X,1);
or
X = mean(X,2);
Honglei Chen
Honglei Chen 2012 年 3 月 14 日
Wayne's answer is for matrix of any size. Just try it out.

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

Sean de Wolski
Sean de Wolski 2012 年 3 月 14 日

1 投票

mean(X(:))
or for speed:
N = numel(X);
sum(reshape(X,N,1))/N;
or for fun:
eval([repmat('mean(',1,ndims(X)) 'X' repmat(')',1,ndims(X)) ';']);

1 件のコメント

Jan
Jan 2012 年 3 月 15 日
A strange definition of "fun".
I think, that "sum(X(:))/numel(X)" is more fun, it is even _nice_.

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

カテゴリ

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

タグ

質問済み:

2012 年 3 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by