Given a positive, scalar integer n, create a (2^n)-by-n double-precision matrix containing the binary numbers from 0 through 2^n-1. Each row of the matrix represents one binary number. For example, if n = 3, then your code could return
>> binary_numbers(3)
ans =
1 1 1 0 0 0 0 1 1 0 1 0 0 0 1 1 0 0 1 1 0 1 0 1
The order of the rows does not matter.
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!
WTF
de2bi() function is not working for this code. Solves the problem when you run it on MATLAB
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.'
用了一个多小时才解决这个
got me one more hour to solve this
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^2-1 x n) matrix but then it asks 32x5 for n=5 ... quite odd
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'
I've just solved this problem by the code that "A=(dec2bin(0:2^n-1)-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 char-vec 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 char-vec. i.e. ---> char('Some Text'+1) = 'Some Text'
The test suite is poorly designed. A better test would be
assert(length(unique(sum(A.*repmat(2.^(0:(n-1)),2^n,1),2)))==2^n)
Why is it not possible to use de2bi?
One ugly line
Can anybody please explain the size ???? I just wrote 1 line !!!
Whats wrong in this ?
Worst Solution ever!!^^
Excellent
lool
function ans = binary_numbers(n)
str2num(dec2bin(0:2^n-1));
end test suit 2 3 fails bit 1 4 5 pass help me
Good Job!
Cant able to understand the problem. Please explain me
how can you come up with such a sotlution,l thought for a long time,still have no shorter solution
The test suite for this solution does not seem to work properly.
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
I have never seen 'ans' used this way before!
no de2bi?
no fullfact?
why not this function ff2n?
They should disallow toolbox functions... this one doesn't work for me!
good answer function!
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 :)
@Jean-Yves, having arrived at the same idea, it is from #4 in the test suite for this problem.
Back to basics 13 - Input variables
205 Solvers
Back to basics 25 - Valid variable names
255 Solvers
Are all the three given point in the same line?
200 Solvers
Generate a string like abbcccddddeeeee
157 Solvers
396 Solvers