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

回答 (1 件)

Image Analyst
Image Analyst 2014 年 3 月 29 日

0 投票

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

カテゴリ

ヘルプ センター および File ExchangeOperators and Elementary Operations についてさらに検索

タグ

質問済み:

H
H
2014 年 3 月 29 日

回答済み:

2014 年 3 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by