Reputation: 63
I'd like to create a pyramid shaped checkerboard on a white background. It needs to be a large matrix (1186,686) or I would just do it manually. A simplified version of what I'm after is below:
0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 1 0 1 0 0 0
0 0 1 0 1 0 1 0 0
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
Upvotes: 0
Views: 751
Reputation: 78790
Here's an attempt, it is half-vectorized since I could not figure out how to do this without a for-loop yet.
function B = board(rows, cols, centerright)
c2 = cols/2;
mid = floor(c2);
if c2 ~= int32(c2) || centerright
mid = mid + 1;
end
B = zeros(rows,cols);
for row = 2:min([rows, mid+1, cols-mid+2])
offset = row-2;
newr = zeros(1, cols);
newr([mid-offset:2:mid+offset]) = 1;
B(row,:) = newr;
end
end
Demo:
>> board(6,9)
ans =
0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 1 0 1 0 0 0
0 0 1 0 1 0 1 0 0
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
>> board(6,10,0)
ans =
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 1 0 1 0 0 0 0
0 0 1 0 1 0 1 0 0 0
0 1 0 1 0 1 0 1 0 0
1 0 1 0 1 0 1 0 1 0
>> board(6,10,1)
ans =
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 1 0 1 0 0 0
0 0 0 1 0 1 0 1 0 0
0 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1
Upvotes: 1