Compute average of each value in a matrix without using for

2 views (last 30 days)
Raldi
Raldi on 31 Oct 2012
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 Comments
Raldi
Raldi on 31 Oct 2012
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.

Sign in to comment.

Accepted Answer

Honglei Chen
Honglei Chen on 31 Oct 2012
you can use conv2 or imfilter, for example
x = magic(3)
y = conv2(ones(3)/9,x)
  5 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!