Is there a more efficient method for this matrix calculation?

1 view (last 30 days)
Is this the most efficient way to do a calculation on a subset of terms in a matrix? For instance if I have a 23x23 matrix and wanted to replace columns 2 through 22 in rows 2 through 22 by the average of the four adjacent terms? This method seems to recalculate the entire matrix at every step rather than just the individual term.
for i=2:22
for j=2:22
H1(i,j)=0.25*(HOLD(i,j+1)+HOLD(i,j-1)+HOLD(i+1,j)+HOLD(i-1,j));
end
end

Answers (1)

Image Analyst
Image Analyst on 29 Mar 2014
Use conv2():
kernel = 0.25 * [0, 1, 0; 1, 0, 1; 0, 1, 0];
H1 = conv2(HOLD, kernel, 'same');

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!