Consider the following code:
L = 4;
sind = Range[-π, (2 π/L)*(L/2 - 1), 2 π/L]
Clear[len]; len = Length[sind];
ix1 = Flatten[Table[sind[[i]], {i, len}, len]]
iy1 = Flatten[Table[Table[sind[[i]], {i, 1, len}], len]]
(* sind={-π, -(π/2), 0, π/2}
ix1={-π, -π, -π, -π, -(π/2), -(π/2), -(π/2), -(π/2), 0, 0, 0, 0, π/2, π/2, π/2, π/2}
iy1={-π, -(π/2), 0, π/2, -π, -(π/2), 0, π/2, -π, -(π/2), 0, π/2, -π, -(π/2), 0, π/2} *)
Now we do a loop over the elements of ix1,iy1
in order to evaluate jnum
:
k = 0;
For[i = 1, i <= Length[ix1], i += len, For[j = 1, j <= len, j++,
jnum = k len + Mod[j, len + 1];
Print[i, " , ", j, " , ", ix1[[i]], " , ", iy1[[j]], " , ", jnum]
]; k = k + 1;]
(* 1 , 1 , -π , -π , 1
1 , 2 , -π , -(π/2) , 2
1 , 3 , -π , 0 , 3
1 , 4 , -π , π/2 , 4
5 , 1 , -(π/2) , -π , 5
5 , 2 , -(π/2) , -(π/2) , 6
5 , 3 , -(π/2) , 0 , 7
5 , 4 , -(π/2) , π/2 , 8
9 , 1 , 0 , -π , 9
9 , 2 , 0 , -(π/2) , 10
9 , 3 , 0 , 0 , 11
9 , 4 , 0 , π/2 , 12
13 , 1 , π/2 , -π , 13
13 , 2 , π/2 , -(π/2) , 14
13 , 3 , π/2 , 0 , 15
13 , 4 , π/2 , π/2 , 16 *)
I want to create a function f
: f[-π ,-π]=1, f[-π ,-(π/2)]=2, ...,f[π/2,π/2] = 16
, i.e I need the x
, y
arguments of f[x_,y_]
be given by the third and fourth columns and the output be the last column (which is given by jnum
inside the loop).
A piecewise definition for every x,y
pair is obviously not the answer. Can it be done using Block
or Module
?