Counting the number of elements surrounding another element.
이전 댓글 표시
Hello,
I've created a matrix and have I randomly set one of these matrix elements equal to 1. Now I want to count how many elements are neighbouring it, I am taking into account diagonal neighbours too i.e. a fully surrounded element will have 8 neighbours etc.
Many thanks,
Phill
댓글 수: 2
Adam
2019년 11월 25일
Just determine if it is on an edge (5 neighbours) or a corner (3 neighbours) and if not then as you say, it has 8 neighbours. No need to count them.
Phillip Smith
2019년 11월 25일
채택된 답변
추가 답변 (1개)
Andrei Bobrov
2019년 11월 25일
편집: Andrei Bobrov
2019년 11월 27일
Let x - your array with ones and zeros:
x = double(rand(10) > .45);
out = conv2(double(~x),[1,1,1;1,0,1;1,1,1],'same');
카테고리
도움말 센터 및 File Exchange에서 Mathematics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!