Make a better code to check the neighbours of a cell in a matrix and replace a value

조회 수: 4 (최근 30일)
Hello, I want to ask for help to make my code better, or maybe simpler. My idea is to check the 8 neighbors of my cell isla(i,j)==2 and if one of them has the value 0, insert in that location the value 2, but only in the first one that the code finds.
My code is:
for i=3:len+2
for j=3:len+2
if isla(i,j)==2
if isla(i,j+1)==0 %espacio a la derecha vacio
isla(i,j+1)=2;
elseif isla(i,j-1)==0 %espacio a la izquierda vacio
isla(i,j-1)=2;
elseif isla(i+1,j)==0 %espacio de abajo vacio
isla(i+1,j)=2;
elseif isla(i-1,j)==0 %espacio de arriba vacio
isla(i-1,j)=2;
elseif isla(i-1,j-1)==0
isla(i-1,j-1)=2;
elseif isla(i-1,j+1)==0
isla(i-1,j+1)=2;
elseif isla(i+1,j+1)==0
isla(i+1,j+1)=2;
elseif isla(i+1,j-1)==0
isla(i+1,j-1)=2;
end
end
end
end
end
Hope you can see the problem is that the code isn't simple. I wish I could use fewer lines to don't have to modify the entire code every time when I have to change the conditions. I hope someone can help me. I apologize if I misspelled a word, English isn't my native language. Greetings

채택된 답변

Image Analyst
Image Analyst 2013년 5월 19일
편집: Image Analyst 2013년 5월 19일
Do you have the Image Processing Toolbox? And are all the numbers guaranteed to be 0 or positive?
If you want any neighbor of a 2 that is a 0 to be set to 2, then don't use elseif - have separate if statements.
if isla(i,j+1)==0 %espacio a la derecha vacio
isla(i,j+1)=2;
end
if isla(i,j-1)==0 %espacio a la izquierda vacio
isla(i,j-1)=2;
end
if isla(i+1,j)==0 %espacio de abajo vacio
isla(i+1,j)=2;
end
if isla(i-1,j)==0 %espacio de arriba vacio
isla(i-1,j)=2;
end
if isla(i-1,j-1)==0
isla(i-1,j-1)=2;
end
if isla(i-1,j+1)==0
isla(i-1,j+1)=2;
end
if isla(i+1,j+1)==0
isla(i+1,j+1)=2;
end
if isla(i+1,j-1)==0
isla(i+1,j-1)=2;
end
  댓글 수: 2
ferne17
ferne17 2013년 5월 19일
Actually I don't want that every neighbour be 2, just the first one that the code can find in an arbitrarian order that I give to search, I'm looking for a more simple code, but aparently they all use Toolbox and do more complex things that what I want.
Image Analyst
Image Analyst 2013년 5월 19일
편집: Image Analyst 2013년 5월 19일
Then put the order of the "if"s in the order that you want, and inside each one put "continue" so that once it gets inside the first "if" it will go to the bottom of the loop and skip all the other ifs. For example:
if isla(i-1,j-1)==0
isla(i-1,j-1)=2;
continue;
end
Do that in all the ifs.

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

추가 답변 (1개)

Ahmet Cecen
Ahmet Cecen 2013년 5월 19일
If I understand correctly from your comment at the previous answer here is a pseudo-code that can do this in a somewhat simpler manner:
ok=0
While ok=0
i= a random number within the bounds of your index (use matlab help to find the function rand)
j= another random number
if isla(i,j)==0 && ( isla(i+1,j)==2 isla(i-1,j)==2 isla(i,j+1)==2 isla(i,j-1)==2 ) %Put as many neighbors within the OR logic as you want.
isla(i,j)=2;
ok=1;
endif
end
  댓글 수: 1
Ahmet Cecen
Ahmet Cecen 2013년 5월 19일
Hmm the ORs didnt show up on the text.
It is supposed to be:
if isla(i,j)==0 AND ( isla(i+1,j)==2 OR isla(i-1,j)==2 OR isla(i,j+1)==2 OR isla(i,j-1)==2 )

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by