Problem 34. Binary numbers
Solution Stats
Problem Comments

7 Comments
matlab functions should not be allowed. Let people think of the solution from scratch.
I actually use a function to do this in my project, but my original code scored 47.
I had all the test cases working properly except for n=10!
This is very weird!!
what's meaning?
when n = 3;
A = binary_numbers(n);
assert(all(A(:) == 0  A(:) == 1))
fun!
Very fun problem!
More difficult than I expected!
Solution Comments

1 Comment
Wow, took my half an hour to solve it without using too many builtin functions.

1 Comment
easy but made me think. nice problem

1 Comment
i can solve it but i have a small mistake

2 Comments
WTF
you win

1 Comment
de2bi() function is not working for this code. Solves the problem when you run it on MATLAB

1 Comment
I correct my comments in the source code.
It was 'Column', not 'Row'.
'Column 1 is 0 1 0 1 ..., column 2 is 0 0 1 1 ... , continue to column n.'

1 Comment
用了一个多小时才解决这个
got me one more hour to solve this

2 Comments
In the third test i.e, for n=5, shouldn't the size (A) be compared to [31 5] instead of [32 5] ?
I didn't even tried to do that, but yeah in example it's 8x3 ( n^21 x n) matrix but then it asks 32x5 for n=5 ... quite odd

2 Comments
Undefined function 'de2bi' for input arguments of type 'double'...
http://uk.mathworks.com/help/comm/ref/de2bi.html ...
de2bi is part of the 'Communications System Toolbox'

3 Comments
I've just solved this problem by the code that "A=(dec2bin(0:2^n1)48);" How it could be that the size of leading solution is 9,but mine is 21 !?
could you pls explain the "48" to me? i see what it does, but dont know why...
fkk,
There is a regexp() trick that some people use to produce very small sized answers. However, they do not necessarily produce a correct solution to the real world problem. I tend to proud that my answers correctly solve the problem, regardless of their size. It is a shame that the regexp() exploit is not discouraged.
Michael Bohm,
When using a numerical operator against a character vector, MATLAB interprets the charvec as a numerical vector with values that correspond to the ASCII values. As such, the ASCII values can be passed through the char() function to produce the original charvec. i.e. > char('Some Text'+1) = 'Some Text'

1 Comment
The test suite is poorly designed. A better test would be
assert(length(unique(sum(A.*repmat(2.^(0:(n1)),2^n,1),2)))==2^n)

1 Comment
Why is it not possible to use de2bi?

1 Comment
One ugly line

1 Comment
Can anybody please explain the size ???? I just wrote 1 line !!!

1 Comment
Whats wrong in this ?

2 Comments
Worst Solution ever!!^^
Excellent

1 Comment
lool

1 Comment
function ans = binary_numbers(n)
str2num(dec2bin(0:2^n1));
end test suit 2 3 fails bit 1 4 5 pass help me

2 Comments

2 Comments
Good Job!
Cant able to understand the problem. Please explain me

1 Comment
how can you come up with such a sotlution,l thought for a long time,still have no shorter solution

1 Comment
The test suite for this solution does not seem to work properly.

2 Comments
Error: Undefined function 'ff2n' for input arguments of type 'double'.
Use of ff2n is no longer allowed; we are not competing in the same league as the first contestants

1 Comment
I have never seen 'ans' used this way before!

1 Comment
no de2bi?
no fullfact?

1 Comment
why not this function ff2n?

2 Comments
They should disallow toolbox functions... this one doesn't work for me!
good answer function!

4 Comments
A masterpiece!
How did you get the idea?
I mean it's certainly short... but pretty inefficient...
what does the 'size' mean? It's ridiculous :)
@JeanYves, having arrived at the same idea, it is from #4 in the test suite for this problem.
Problem Recent Solvers3306
Suggested Problems

Extract leading nonzero digit
1556 Solvers

309 Solvers

Find perfect placement of nonrotating dominoes (easier)
297 Solvers

Create an indexpowered vector
479 Solvers

759 Solvers
More from this Author95
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!