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; endend endendend
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
Best Answer