How can I add four neighbor elements (left, right, top and bottom) of each element of a given matrix and make a new matrix of the same sixe from the calulated values?

11 views (last 30 days)
For example, let A= [ 0 2 1; 1 0 1; 1 0 2]. Then my resulting matrix should be [3 1 3; 1 4 3; 1 3 1].

Accepted Answer

Image Analyst
Image Analyst on 16 Nov 2015
Use conv2():
A= [ 0 2 1; 1 0 1; 1 0 2]
kernel = [0,1,0;1,0,1;0,1,0]; % Sum above, left, right, and below
B = conv2(A, kernel, 'same')

More Answers (0)

Community Treasure Hunt

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

Start Hunting!