Is there a more efficient method for this matrix calculation?

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

Réponses (1)

Use conv2():
kernel = 0.25 * [0, 1, 0; 1, 0, 1; 0, 1, 0];
H1 = conv2(HOLD, kernel, 'same');

Catégories

En savoir plus sur Operators and Elementary Operations dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by