MATLAB: Can I assign values to an array in a diagonal direction from a reference point on that array

array chess matrix diagonal board for loop

If I have an array of size NxN,called X, and I count through it using an (i,j) for loop like this
for i = 1:N
for j = 1:N
and im looking for a value in it like this
if(X(i,j) == 1)
I want to start where I find a 1 and change the diagonal lines around it to an 8. So far ive tried code similar to this and many things like it but it doesnt seem to work how I envision it
for i = 1:N
for j = 1:N
if(X(i,j) == 1)
X(i+1,j+1) = 8;
X(i-1,j-1) = 8;
X(i+1,j-1) = 8;
X(i-1,j+1) = 8;
end
end
end
I want the outcome to be something like
0 0 0 0 0
0 0 0 0 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 0
this turns into this
8 0 0 0 8
0 8 0 8 0
0 0 1 0 0
0 8 0 8 0
8 0 0 0 8

Best Answer

Perhaps:
X = [...
0 0 0 0 0 0
0 0 0 0 0 0
0 0 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 1 0 0
0 0 0 0 0 0
0 0 0 0 0 0
];
[R,C] = find(X);
for k = 1:numel(R)
r = R(k);
c = C(k);
X([r-1,r+1],[c-1,c+1]) = 8;
X([r-2,r+2],[c-2,c+2]) = 8;
end
giving:
>> X
X =
8 0 0 0 8 0
0 8 0 8 0 0
0 0 1 0 0 0
0 8 0 8 0 0
8 0 0 0 8 0
0 8 0 0 0 8
0 0 8 0 8 0
0 0 0 1 0 0
0 0 8 0 8 0
0 8 0 0 0 8