Compute average of each value in a matrix without using for

Hi all,
I was wondering is it possible to alter each value of a matrix based on the average value of its neighbors, wihtout using for loops?
Thanks in advance.

2 件のコメント

the cyclist
the cyclist 2012 年 10 月 31 日
It would eliminate a lot of guesswork on the part of people trying to help you if you could be much more specific about what you want to do, and ideally provide at least one example of the input and output you would expect from this.
Raldi
Raldi 2012 年 10 月 31 日
You are right...
Well basicaly lets say i have
x= [0 0 0 0 0 0 0 2 0 0 0 0 0 1 0 0 0 0 0 0]
What i want is to create a new matrix that will be
newx= [0 0 (2/9) 0 0 0 0 2 0 0 0 0 (3/9) 1 0 0 0 0 0 0]
Here i randomly changed only two values to show that the get the average value of all the neighboring values.

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

 採用された回答

Honglei Chen
Honglei Chen 2012 年 10 月 31 日

0 投票

you can use conv2 or imfilter, for example
x = magic(3)
y = conv2(ones(3)/9,x)

5 件のコメント

Raldi
Raldi 2012 年 10 月 31 日
Thanks...
Matt J
Matt J 2012 年 10 月 31 日
編集済み: Matt J 2012 年 10 月 31 日
You can do this slightly more efficiently by taking advantage of separability
h=ones(3,1)/3;
y=conv2(x,h,h);
Andrei Bobrov
Andrei Bobrov 2012 年 10 月 31 日
may be
y=conv2(h,h,x);
Matt J
Matt J 2012 年 10 月 31 日
Yes, that's what I really meant.
Raldi
Raldi 2012 年 10 月 31 日
Thanks alot guys.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および 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