How to replace an element with average of surrounding elements excluding that particular element?

조회 수: 3 (최근 30일)
This is similar to
A=[1 2 3; 4 5 6; 7 8 9];
conv2(A, ones(3), 'same') ./ conv2(ones(size(A)), ones(3), 'same');
ans =
3.0000 3.5000 4.0000
4.5000 5.0000 5.5000
6.0000 6.5000 7.0000
But this includes the central elements also. I want to exclude those elements and replace the elements by average of surrounding elements only. e.g. 1 should be replaced by 3.67, not 3.

채택된 답변

Walter Roberson
Walter Roberson 2022년 5월 28일
If not for boundary conditions, you could use
[1 1 1; 1 0 1; 1 1 1]/8
For boundary conditions, consider what happens if you conv2(ones(size(A)), ones(3),'same')
  댓글 수: 1
Shaunak Chatterjee
Shaunak Chatterjee 2022년 5월 28일
I think this may work.
window=3;
convwindow=ones(window);
convwindow(round(window/2),round(window/2))=0;
conv2(A, convwindow, 'same') ./ conv2(ones(size(A)), convwindow, 'same');
This is working for my matrix.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by