# Explain the average of 8 nearest neighbors

8 ビュー (過去 30 日間)
A R 2020 年 3 月 31 日
コメント済み: A R 2020 年 4 月 3 日
Hi, for dead pixel replacement I came across this code from MATLAB file exchange,https://in.mathworks.com/matlabcentral/fileexchange/4551-inpaint_nans . It takes average of 8 nearest neighbors.
% % generate sparse array to average 8 nearest neighbors
% % for each nan element, be careful around edges
fda=spalloc(n*m,n*m,size(nan_list,1)*9);
% -1,-1
L = find((nan_list(:,2) > 1) & (nan_list(:,3) > 1));
nl=length(L);
if nl>0
fda=fda+sparse(repmat(nan_list(L,1),1,2), ...
repmat(nan_list(L,1),1,2)+repmat([-n-1, 0],nl,1), ...
repmat([1 -1],nl,1),n*m,n*m);
end
% eliminate knowns
rhs=-fda(:,known_list)*A(known_list);
% and solve...
B=A;
k=nan_list(:,1);
B(k)=fda(k,k)\rhs(k);
Can you please explain what is [-1, -1]and how the neighbor and its average is calculated in the above code.

サインインしてコメントする。

### 回答 (1 件)

Steven Lord 2020 年 3 月 31 日
Consider the following matrix. Each element is equal to its linear index.
>> nRows = 4;
>> nCols = 6;
>> A = reshape(1:(nRows*nCols), [nRows, nCols])
A =
1 5 9 13 17 21
2 6 10 14 18 22
3 7 11 15 19 23
4 8 12 16 20 24
What are the eight neighbors of 18?
Can you find a way to calculate the indices of the neighbors of a point given nothing but the index of the center point and the values nRows and nCols? [Assume that the center point is not on one of the edges of A.]
The left neighbor of C is ...
The right neighbor of C is ...
Repeat for the other six neighbors. Try using the formulae to calculate the indices of the neighbors of a different element, say 11.
##### 1 件のコメント表示非表示 なし
A R 2020 年 4 月 3 日
Hello Steven, thanks a lot for your response. Can you please ellaborate the steps in the above program, how the 8 neighbors values are found .

サインインしてコメントする。

### カテゴリ

Find more on Spatial Search 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!

Translated by