# Combine two color schemes for coloring elements in matrix, based on their positions (i,j)

I want to color one of the diagonal elements of a matrix that consists of 45° rotated and zigzag arranged squares. The addressing condition is following j=2*i and j=2*i-1, where j and i are the vertical and horizontal order numbers.

sideLength = 1;
rotatedSqare[x0_, y0_] := Polygon[{{x0, y0}, {x0 + sideLength*0.7,
y0 - sideLength*0.7}, {x0 + sideLength*1.4,
y0}, {x0 + sideLength*0.7, y0 + sideLength*0.7}}]


Applying the conditions individually If [j == 2*i , Blue, Green] and If [j == 2*i - 1 , Blue, Green] for coloring works properly, as shown below:

{Graphics[{EdgeForm[Thickness[0.0013]], LightBlue, Table[{If [j == 2*i -    1 , Blue, Green], rotatedSqare[
x0 + (period)*(i - 1) + Boole[OddQ[j + 1]]*(period)/2,
y0 + (period/2) j]}, {i, 1, 11}, {j, 1, 22}]}, PlotRange -> {{0, 20}, {0, 15}}, ImageSize -> 300], Graphics[{EdgeForm[Thickness[0.0013]], LightBlue, Table[{If [j == 2*i , Blue, Green], rotatedSqare[
x0 + (period)*(i - 1) + Boole[OddQ[j + 1]]*(period)/2,
y0 + (period/2) j]}, {i, 1, 11}, {j, 1, 22}]}, PlotRange -> {{0, 20}, {0, 15}}, ImageSize -> 300]}


When I combine two conditions together “ If [(j == 2*i And j == 2*i-1), Blue, Green]”, then I get this message:

“If is not a Graphics primitive or directive.”

Can someone help me to get around this? Thanks!!!

• If you entered the same code as you gave in text, then you have probably a syntax error. You can replace that And with && or use And with infix notation as If[(j == 2*i)~And~(j == 2*i - 1), Blue, Green]. – Pinti May 14 '17 at 18:15
• You have also forgot to include the definitions for symbols x0, y0 and period in your code. Please edit and update your question accordingly. – Pinti May 14 '17 at 18:17
• Thanks a lot Pint for repspondingi!!! – surfAliq May 15 '17 at 5:04
• @Pinti. Thanks a lot Pint for repspondingi!!! Yes I forgot the parameters, here they are x0 = 0; y0 = 0; sideLength = 1; period = 1.5*sideLength. – surfAliq May 15 '17 at 5:11
• @Pinti. I have tried both If [(j == 2*i - 1) && (j == 2*i), Blue, Green] and If [(j == 2*i - 1)~And~ (j == 2*i), Blue, Green]. It does not give any error message, but does not do apply the conditions. – surfAliq May 15 '17 at 5:30

here is the result:

x0 = 0; y0 = 0;
sideLength = 1;
period = 1.5*sideLength;

rotatedSqare[x0_, y0_] := Polygon[{{x0, y0}, {x0 + sideLength*0.7, y0 - sideLength*0.7}, {x0 + sideLength*1.4, y0}, {x0 + sideLength*0.7, y0 + sideLength*0.7}}]

Graphics[{EdgeForm[Thickness[0.0013]], LightBlue, Table[{If [(j == 2*i - 1) || (j == 2*i), Blue, Green], rotatedSqare[
x0 + (period)*(i - 1) + Boole[OddQ[j + 1]]*(period)/2,
y0 + (period/2) j]}, {i, 1, 20}, {j, 1, 22}]}, PlotRange -> {{0, 20}, {0, 15}}] 