Issue with mean function

1 回表示 (過去 30 日間)
Shonfield
Shonfield 2012 年 5 月 21 日
Hi,
I am wanting to make each element in an array (excluding the first and last rows & columns, i.e. the elements on the edge) the average of that element and the 8 surrounding elements using the mean() function. The mean function is currently returning a 1x125 matrix where each element is the average of the elements in each corresponding column in the original 125x125 matrix. Is there a way I can make the mean function return a 125x125 matrix (the same size as the original matrix to be averaged)? It's for a uni assessment and we're supposed to use the mean function.
Cheers, Bruce
  1 件のコメント
Daniel Shub
Daniel Shub 2012 年 5 月 21 日
Although by a different user, this is a duplicate of http://www.mathworks.com/matlabcentral/answers/38766-averaging-matrix-and-store-it-in-a-new-matrix

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

回答 (2 件)

Titus Edelhofer
Titus Edelhofer 2012 年 5 月 21 日
You will need to do exactly what you are describing: taking for each element the mean of the surrounding elements. What you did was to compute the mean of the entire matrix ...
Titus

Sean de Wolski
Sean de Wolski 2012 年 5 月 21 日
Sure, you could do this with mean() and a couple of for-loops but conv2() is way cooler:
xm3x3 = conv2(x,ones(3)*mean([0 1/9 2/9]),'same');
  2 件のコメント
Image Analyst
Image Analyst 2012 年 5 月 21 日
Of course he has conflicting requirements. "(excluding the first and last rows & columns, i.e. the elements on the edge) " means that he want the 'valid' option of conv2(), while "(the same size as the original matrix..." means that he wants the 'same' option of conv2().
Sean de Wolski
Sean de Wolski 2012 年 5 月 21 日
I saw that. When in doubt, go same, it's easier to extract than create.

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

カテゴリ

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