I've got a large matrix, mainly consisting of zeros and with some randomly placed values. I would like to be able to find the surrounding zeros of the non-zero values. If a single value is only surrounded by zeros it's no problem, but a cluster of values poses more problems. I could treat the values in the cluster as single values, but that means an extremely long runtime of the code. Is there a way to find all zeros surrounding a certain value? I don't have any additional toolboxes, but if there is one that could be usefull, I can always try to convince my boss that I need it...

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 2월 4일

0 개 추천

bwdist from Image Processing Toolbox
OR
A - your array
[i1,j1] = find(A)
[y x] = ndgrid(1:size(A,1),1:size(A,2))
out = min(max(abs(bsxfun(@minus,x,reshape(j1,1,1,[]))),...
abs(bsxfun(@minus,y,reshape(i1,1,1,[])))),[],3)

댓글 수: 4

M.J. Glasbergen
M.J. Glasbergen 2012년 2월 8일
The 'matlab' function does not function completely the same as the bwdist, it also marks the diagonally touching cells (distance (sqrt(2)) as 1. Is there a way to mark only the cells touching side by side as 1 without the Image Processing Toolbox?
Andrei Bobrov
Andrei Bobrov 2012년 2월 8일
out = conv2(A+0,[0 1 0;01 0 1;0 1 0],'same').*A~=0
Andrei Bobrov
Andrei Bobrov 2012년 2월 8일
more variant
out = min(hypot(bsxfun(@minus,y,permute(i1,[3 2 1])),bsxfun(@minus,x,permute(j1,[3 2 1]))),[],3)
as 'bwdist' from 'Image Processing Toolbox'
M.J. Glasbergen
M.J. Glasbergen 2012년 2월 10일
You're the greatest, this last one is the one I was looking for now :-)

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

추가 답변 (1개)

M.J. Glasbergen
M.J. Glasbergen 2012년 2월 5일

0 개 추천

Seems to work great, thnx!

제품

태그

질문:

2012년 2월 4일

편집:

2013년 10월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by