# matrix pattern

hi,

i’ve got a matrix grid that changes size, currently its 10 x 10 but it can be 20 x20, or 5 x 5 so changable

i was wondering how to get a pattern like this. Using some sort of a formula

lets say that we have an

``````int gridColour =0;
``````

if it’s purple `gridColour = 1;` and if its cyan/blue `gridColour = 0;`

int pattern1 = new int
{
0,0,0,0,0,0,0,0,0,0,
0,1,1,1,1,1,1,1,1,0,
0,1,0,0,0,0,0,0,1,0,
0,1,0,1,1,1,1,0,1,0,
0,1,0,1,0,0,1,0,1,0,
0,1,0,1,0,0,1,0,1,0,
0,1,0,1,1,1,1,0,1,0,
0,1,0,0,0,0,0,0,1,0,
0,1,1,1,1,1,1,1,1,0,
0,0,0,0,0,0,0,0,0,0,
};

``````matrixArr[ix,iy] = pattern1[iy*rand + ix];
``````

This should work, but it’s untested.
Basically if you wanna pass a stored pattern you have to be able to parse it somehow, this is only one of many examples.

right so i have found a solution (this works in my script, may have been some errors as i had to change some variable names)

``````private int[,] matrixArr;
int rand =0;
string displayResult="";
void Start ()
{

rand = (int)(Random.Range (5, 20));
matrixArr = new int[rand, rand ];

for (int ix = 0; ix < rand; ix++) {
for (int iy = 0; iy < rand; iy++) {
setColour (ix, iy);
displayResult+=matrixArr [ix, iy];
}
displayResult+="
``````

";
}

``````		Debug.Log(displayResult);
}

void setColour (int ix, int iy)
{
for (int i=0; i < rand; i++) {
if (ix == i && iy >= i && iy < rand - 1 - i || iy == i && ix >= i && ix < rand - 1 - i
|| ix == rand - 1 - i && iy >= i && iy <= rand - 1 - i || iy == rand - 1 - i && ix >= i && ix <= rand - 1 - i) {
matrixArr [ix, iy] = i % 2;

break;
}
}
}
``````