Given an integer n, make an n-by-n matrix made up of alternating ones and zeros as shown below. The a(1,1) should be 1.
Example:
Input n = 5 Output a is [1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1]
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
Missing a line?
a = zeros(n)
The following solution, which is correct, is marked wrong.
function a = checkerboard(n)
a = ones(n);
a(2:2:end) = 0;
end
lol
a = ~rem((1:n)+(1:n)',2) % Size: 23
puzzle me a little, but finally I solved it in a brief way.
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
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
Sick
Anyone know why this isn't correct? works fine on matlab for me:(
Look at your result carefully when n = 4.
MANTAP JAYA!!!!!!!!!!
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.
good
This is so cool !!!
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".
I believe this is one of the best "legit" solutions to this problem. Good job.
Very clever!
summary of this annoying problem:
method size(min), invhilb 14, toeplitz 17, hankel 23, repmat with eye 24, meshgrid 25, 1-D for 28, 2-D for 31, ones assign value 36, reshape 38
LETSGOOOO
please don't use 'for...end'.It is recommended in matlab.
Tried to find the shortest solution by using MOD(X,2) function. Which one is three times better?
Don't know if using bsxfun is allowed but this works
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.
The program submits the proper solution. The assertion fails, why is this?
Not sure why thisis not working here while working on my comp.
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.
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
Improved by 12 by just adding the regexp cheat.
This solution uses the toeplitz function
hehe
Well played. Didn't think of using meshgrid.
good job!
could be nice to be able to see better solutions..
I love it make things better by "sweeping things under the rug!" i.e. hiding all calculations in other functions....
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 even-sized array, when you go from row 1 to row 2, the sequence has two zeros in a row instead of alternating zeros and ones.
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!!
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
The Solution is incorrect since it is static only works for n=4 and n=5
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].
The Solution is incorrect for all even values of n greater than 4...
The debugged code is:
http://www.mathworks.com/matlabcentral/cody/problems/4-make-a-checkerboard-matrix/solutions/299777
like it, simplicity!
Elegant solution
Very clever!
Nice
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.
Why is this wrong? They look like checkerboards to me...
It looks like you've got an off-by-one problem. When n = 2, your code returns a 3-by-3 checkerboard, and so on.
Thanks! Looks like my solution sucks anyways according to the count
Execute it for n = 9.
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.
How is this solution wrong?!
when n is even like n=4 , a is not the expected output
So brilliant!
Very clever.
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 top-level 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.
This should not be allowed.
Nice binary shower.
It works doesn't it? Haha.
2577 Solvers
Set the array elements whose value is 13 to 0
758 Solvers
3159 Solvers
Find out sum of all elements of given Matrix
276 Solvers
Spot the First Occurrence of 5
310 Solvers