finding neighbor value
显示 更早的评论
Hi,
suppose I have a large matrix A of size 200X200
I need to know the neighbor value and also the neighbor position of distance 3 of the position (50,50)
can it be done easily
A =
1 2 3
3 3 6
4 6 8
4 7 7
here (2,2) value is 3. its one distance neighbor is 1,2,3,3,6,4,6,8 and position is (1,1),(1,2),(1,3),(2,1),(2,3), (3,1),(3,2),(3,3)
采纳的回答
更多回答(1 个)
Oleg Komarov
2011-5-12
EDITED
To make it flexible n = distance from center
A = randi(170,17,13);
center = [8 7];
n = 2;
% Boundary check
if all(pos - n) && all(pos + n <= size(A))
B = A(center(1)-2:center(1)+2, center(2)-n:center(2)+n).';
B = B([1:2*n*(n+1) 2*(n^2 + n + 1):end]);
end
类别
在 帮助中心 和 File Exchange 中查找有关 Nearest Neighbors 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!