Problem 4. Make a checkerboard matrix
Solution Stats
Problem Comments

36 Comments
This problem is far less trivial than it seems. Fun!
Cool.
Nice
Good one
All solutions with score 10 or 11 use the regexp cheat.
nice one
nice
I like this one! came to know about the invhilb function just because of this!
simple Tricky
This solution works on matlab but not here.
function a = checkerboard(n)
theta=pi/2:pi/2:n*pi/2;
a = (sin(theta)'*sin(theta))+cos(theta)'*cos(theta);
end
very good
This is a good problem!
Very nice! ;)
nice!
It is good problem!
good
Good!
Good
Good one
good one
I can not ...
nice problem
Nice
Puzzled me a bit but I'm proud of the final result even if i did 47 lines of code for this haha!
Nice
Nice  I can think of a few ways to do this. I dislike for loops in general so i try to minimise use, but ended up with a single reduced for loop and an eye commands. Enjoy!
function a = checkerboard(n)
a = ones(n);
a(2:2:n^2)=0;
end this code give me right answer in soft,but failed here
Good problem. Helps to understand the uses of logical operators.
The even part n=4 is tricky, Hint: google it
%% cherboard
function [board] = Chekerboard(n)
%% set bord matrix with zeros
board = zeros(n,n);
for j = 1:n
if mod(j,2)==0 %if zero row is even
for i =1:2:n
board(j,i) = 1;
end
else mod(j,2)==1 %if 1, row is odd
for t = 2:2:n
board(j,t) = 1;
end
end
end
this works and easy to understand~
function a = checkerboard(n)
if mod(n,2)==1
b = ones(n,n);
for i=2:2:n^2
b(i)=0;
end
a=b;
else
b = ones(n+1);
for i=2:2:(n+1)^2
b(i)=0;
end
a=b(1:n,1:n);
end
end
I just made
a(2:2:n,1:2:n) = 0;
a(1:2:n,2:2:n) = 0;
rather easy actually
What is the "size" of the solved problem? How is it calculated?
Awesome!
Nice problem :)
Fun problem with different solutions!
Solution Comments

1 Comment
Good problem but the solution check needs to be more accurate or more tests.

1 Comment
such a good practice

1 Comment
Missing a line?
a = zeros(n)

4 Comments
The following solution, which is correct, is marked wrong.
function a = checkerboard(n)
a = ones(n);
a(2:2:end) = 0;
end
lol
Maybe it is right for a determined "n", but for other values it's wrong, that is why it does several tests.
not valid for even value of n. plz recheck it

5 Comments
Phillippe, please delete this solution so we can see the real best answer
function a = checkerboard(n)
b=ones(n);
for i=1:n
for j=1:n
if (mod(i+j,2))
b(i,j)=0;
end
end
end
a=b
end
Phillippe seems like crackbrained with being "leading solutioner".
Where is the best solution?
function a = checkerboard(n)
t1 = ones(n,1) * (1:n);
t = t1 + t1';
a = bitget(t+1, 1);
end
function a = checkerboard(n)
a = zeros(n);
a(1:2:n,1:2:n)=1
a(2:2:n,2:2:n)=1
end

1 Comment
a = ~rem((1:n)+(1:n)',2) % Size: 23

1 Comment
puzzle me a little, but finally I solved it in a brief way.

2 Comments
we are getting failed even though the code works
this works
function a = checkerboard(n)
b = ones(n,n);
for i=2:2:n^2;
b(i)=0;
end
a=b
end

3 Comments
we wrote
t = n*n
s = 1:1:t
u = mod(s, 2)
a = reshape(u, [n, n]);
which gives the right matrix in matlab. So, why did we get a fail?
:O(
you failed with even cases
incorrect for checkerboard(2)

1 Comment
Sick

2 Comments
Anyone know why this isn't correct? works fine on matlab for me:(
Look at your result carefully when n = 4.

1 Comment
MANTAP JAYA!!!!!!!!!!

2 Comments
very easy
Well done. For your interest, the second loop is not required, as "mod(i+2,2)+1:2:n" can be used directly as an index.

1 Comment
good

1 Comment
This is so cool !!!

2 Comments
I do not see what is wrong with my solution?
Consider the first iteration (I=1 & j=1). The variable "value" was initialised, so the first conditional test is true, hence this matrix element is set to 1 (no change!) and "value" is set to 0. However, instead of finishing this iteration (refer to the "continue" statement), a second "if" statement is encountered, and because "value" is now 0, this second conditional is also true, so the first matrix element is finally changed to 0!
You would have better luck with a single if statement that included "else".

1 Comment
I believe this is one of the best "legit" solutions to this problem. Good job.

1 Comment
Very clever!

1 Comment
summary of this annoying problem:
method size(min), invhilb 14, toeplitz 17, hankel 23, repmat with eye 24, meshgrid 25, 1D for 28, 2D for 31, ones assign value 36, reshape 38

1 Comment
LETSGOOOO

1 Comment
please don't use 'for...end'.It is recommended in matlab.

1 Comment
Tried to find the shortest solution by using MOD(X,2) function. Which one is three times better?

1 Comment
Don't know if using bsxfun is allowed but this works

2 Comments
function a = tests(n)
a = ones(n);
a(2:2:numel(a)) = 0;
end
Any ideas why this only works on matreices with an odd number of rows/columns? On the even matrices it changes entire rows to 0's or 1's
the ones and zeroes should be alternate whatever their position be. In even columns/rows the element a(1,1) and a(1,2) both remain the same i.e. 1 and 1 so it doesn't work for even number of columns\rows.

1 Comment
The program submits the proper solution. The assertion fails, why is this?

1 Comment
Not sure why thisis not working here while working on my comp.

3 Comments
There has to be a better way
This one works on matlab but not here
function a = checkerboard(n)
theta=pi/2:pi/2:n*pi/2;
a = (sin(theta)'*sin(theta))+cos(theta)'*cos(theta);
end
hey mec.The function you which you have created gives negative one's(data type double) in the upper triangular and lower triangular part whereas the output should be all positive so first convert to data type integer using int function then convert to one's and zero's using logical.

5 Comments
Not very small, but a fun and different approach I think.
Nicely done!
Interesting one!
Nice use of recursion !!
Nice one, really wanna find the shortest answer

1 Comment
Improved by 12 by just adding the regexp cheat.

1 Comment
This solution uses the toeplitz function

1 Comment
hehe

2 Comments
Well played. Didn't think of using meshgrid.
good job!

1 Comment
could be nice to be able to see better solutions..

1 Comment
I love it make things better by "sweeping things under the rug!" i.e. hiding all calculations in other functions....

2 Comments
Why is this solution getting the wrong values on the second solution set yet when I input it into MatLab to test it they are correct?
With an evensized array, when you go from row 1 to row 2, the sequence has two zeros in a row instead of alternating zeros and ones.

6 Comments
Tricky solution!
How do I view solutions? Everything is locked. It says I may solve ANOTHER problem to unlock, but I keep solving and everything is still locked. Not terribly helpful.
You have to solve additional problems and then come back to look at the solutions here.
Nice and simple "math" solution!
if i can do that i dont need to view this solution,what a stupid rule
so stupid rule!!

1 Comment
For n >= 15 the computation time of invhilb is very low. It would be interesting if the invhilb function could be forced to approximate the answer, instead of exact integer responses for n < 15 which slow it down

1 Comment
The Solution is incorrect since it is static only works for n=4 and n=5

3 Comments
Why this not works?????
a=repmat(eye(2),n);
Because you apparently cannot get odd matrix dimensions with the repmat command.
The output is twice the required size  the repmat function repeats the given matrix n times. So repmat(eye(2),1) gives [1 0;0 1], not [1].

1 Comment
The Solution is incorrect for all even values of n greater than 4...
The debugged code is:
http://www.mathworks.com/matlabcentral/cody/problems/4makeacheckerboardmatrix/solutions/299777

1 Comment
like it, simplicity!

3 Comments
Elegant solution
Very clever!
Nice

2 Comments
Why is this wrong? I produced a 5x5 checkerboard matrix when n=5 and a 4x4 checkerboard matrix when n=4.
This is because you have semicolon beside if statement and also you are checking only the second element of each row.

3 Comments
Why is this wrong? They look like checkerboards to me...
It looks like you've got an offbyone problem. When n = 2, your code returns a 3by3 checkerboard, and so on.
Thanks! Looks like my solution sucks anyways according to the count

1 Comment
Execute it for n = 9.

1 Comment
H = invhilb(n) generates the exact inverse of the exact invhilb(n) gives Hilbert matrix for n less than about 15. For larger n, invhilb(n) generates an approximation to the inverse Hilbert matrix.

2 Comments
How is this solution wrong?!
when n is even like n=4 , a is not the expected output

2 Comments
So brilliant!
Very clever.

2 Comments
As far as I can tell, the output my solution provides is identical to the one provided by the example. Why is this not being counted as correct?
You're calling "checkerboard(1, n, n)" in the function "checkerboard(n)" Your first call has three arguments for checkerboard. The toplevel function has only one. Therefore, you're trying to call checkerboard with too many input arguments.
Hopefully, a late answer is better than none at all.

1 Comment
This should not be allowed.

2 Comments
Nice binary shower.
It works doesn't it? Haha.
Problem Recent Solvers14699
Suggested Problems

2054 Solvers

1219 Solvers

Arrange Vector in descending order
8480 Solvers

418 Solvers

Convert from Fahrenheit to Celsius
17529 Solvers
More from this Author96
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!