mardi 30 mai 2017

How can I use an if-else statement to select elements in different matrices?

I have been stuck on the following issue for days and would appreciate any help. I have 2 different matrices, A and B. I have a starting x and y position (xpos, ypos), and want to first, identify whether there is an element within a certain range that has a value of "0" in the matrix B. If there is an element of such kind, I want the x and y position of this element to be the new x and y position. If there aren't any elements with a value of "0", I want to select the element with the highest value within a certain range from the starting positions in matrix A. The position and this element then becomes the new x and y position. I have the following code:

for i=1:5
    range=5
    xpos=100
    ypos=100
    xstart=xpos-range
    ystart=ypos-range

    for gg=1:10; %double the range
        for hh=1:10;

            if  ystart+gg <= 652 && ystart+gg>0 && xstart+hh <= 653 &&  xstart+hh> 0 &&  B(xstart+hh,ystart+gg)== 0;

                xpos = xstart+hh %this becomes the new xposition
                ypos = ystart+gg

            else

                if  ystart+gg <= 652 && ystart+gg >0 && xstart+hh <= 653 && xstart + hh >0 && B(xstart+hh,ystart+gg)~= 0;



                    if  ystart+gg <= 652 && ystart +gg>0 && xstart+hh <= 653 && xstart+hh>0 && A(ystart + gg, xstart +hh) >= 0.0;
                        maxtreecover = A(ystart + gg, xstart + hh)

                        xpos = xstart + gg %this becomes the new xpos
                        ypos = ystart + hh %this becomes the new ypos

                    end
                end
            end
        end
    end
end

The problem with this is that it does not search ALL of the elements within the range for a "0" (in the B matrix) before moving into searching the A matrix. How can I modify this code to reflect what I intend it to do?

Aucun commentaire:

Enregistrer un commentaire