Question: Problem with indices in Maple for the for/if commands

I have a function which calculates a number for [x,y] inside a matrix, which depends on the values surrounding said initial cell (from [x-1,y-1] yo [x+1,y+1]). However, for the values on the border, the idea is to go around to the opposite side, creating a Torus of the sort.
The solution I seemed to have found was to create 4 indices: 2 controled with 'for' and 2 which were modified accordingly to be used as imputs for the function:

x:=x0;y:=y0;
for r from x-1 to x+1 do
    print("r",r);
    for t from y-1 to y+1 do
        print("t",t);
        if r=0 then i:=N; else i:=r; fi;
        if t=0 then j:=N; else j:=t; fi;
        if r=N+1 then i:=1; else i:=r; fi;
        if t=N+1 then j:=1; else j:=t; fi;
        print("i",i);print("j",j);
        function(i,j);
od; od;
 

(the print commands are for the tests I have been running so see where the potential error lied).
If you run this script, you can clearly see for x0,y0<>1 that it works perfectly, even for x0,y0=N where it succesfully goes around and changes to 1. However, for x0,y0=1 it isn't able to detect that r&t=0 and so it doesn't change it to N; creating an error where it tries to find the value 0 of an Array.

Thanks in advance,
Enrique

Please Wait...