11

I want to create a matrix in matlab with 500 cell (50 row,10 column ), How I can create and initialize it by random binary digits? I want some thing like this in 50*10 scale as sample 3*4

0 1 1 1
0 0 0 0
1 1 1 1

and after it, how can get decimal equation of any row ? like row 1 is equal 7 in decimal

Yuseferi
  • 7,931
  • 11
  • 67
  • 103

5 Answers5

12

Why not use randi to generate random integers?

A = randi([0 1], 50, 10);

and to convert a binary row to a number - as in the previous answers:

bin2dec(num2str(A(n,:)))
angainor
  • 11,760
  • 2
  • 36
  • 56
  • 1
    Because ints are ints, and bools are bools. It's always best to stick to type, even in Matlab :) – Rody Oldenhuis Oct 14 '12 at 07:51
  • 1
    @RodyOldenhuis OP wanted binary digits, not *true* and *false*. That means numbers, not logical values. – angainor Oct 14 '12 at 08:22
  • Look [here](http://en.wikipedia.org/wiki/Bit) for the definition of a "binary digit" :) `true` and `false` are simply different words for `1` and `0`, respectively. – Rody Oldenhuis Oct 14 '12 at 08:25
  • @RodyOldenhuis you generate random doubles and cast them to logicals. Can I do the same, just using proper function for generations of random integers from [0 1]? I asked about using `randi` as opposed to `rand`. In your method, are both digits found with equal probability? – angainor Oct 14 '12 at 09:15
  • Yes, since `rand` chooses uniformly from `[0,1)`, the command `rand>0.5` returns a `true` or `false` with equal probability. Note that in C/C++ you'd generate random doubles [like here](http://stackoverflow.com/questions/2704521/generate-random-double-numbers-in-c), wheras random ints are done like [here](http://www.cplusplus.com/reference/clibrary/cstdlib/rand/). Note also the remark a few lines down from the top of that last link. It's anyone's guess if the Mathworks have used C or Fortran for the implementation of `rand` and `randi`, but given these two links, `rand` seems a safer bet. – Rody Oldenhuis Oct 14 '12 at 10:36
  • If you want logicals you'll have to cast them too: `logical(randi(...))`. You can cast, I have to compare -- you'll be faster :) BTW, in the past, you could have suggested `randint(50,10)`, a bit less versatile but cleaner in this context, but that's deprecated now... – Rody Oldenhuis Oct 14 '12 at 10:38
  • ooh! Doing a speed test, my solution is (quite unexpectedly I have to say) almost 3 times faster than yours... – Rody Oldenhuis Oct 14 '12 at 10:42
  • @RodyOldenhuis [`rand` documentation](http://www.mathworks.se/help/matlab/ref/rand.html) and `rand` help both say that it chooses numbers from an open interval `(0,1)`, so I think you are wrong. About the cast, I would never bother to do it, since I do not care whether my 0/1 is expressed as 4 or 1 byte integers just to print them later. I would have to see some speed benefits, otherwise storage is just storage to me. About speed of `rand` vs. `randi`, you may well be right, I never checked. But in this case you did not convince me that the results are the same, so I go for correctness :) – angainor Oct 14 '12 at 11:44
  • How does the openness of the interval affect my statement? You're really nagging over 1 in 1/eps difference in probability?! Then the burden of proof now lies with you to show that randi does that differently (*demonstrate*, not link to te doc :) – Rody Oldenhuis Oct 14 '12 at 12:33
  • @RodyOldenhuis cmon, it's Sunday:) I'm not nagging, and I don't want to prove it, because I don't really want to think about it every time I randomise integers. I play safe and hold mathworks responsible for their code and documentation. And I did show that 0 and 1 have different probability the way you do it, be it eps or not.. You may not care, I probably don't really care :) but maybe others do. It is useful to know the pitfalls. A loose analogy - recently scientists thought they found faster than light neutrinos. For some faulty time measurement device and a lost 60 ns. irrelevant,I know:) – angainor Oct 14 '12 at 12:50
  • Not sure why you think you *proved* it -- the situation is a lot more complicated, see for example [this question](http://stackoverflow.com/questions/2978930/how-many-double-numbers-are-there-between-0-0-and-1-0/). Anyway, doing a `sum(rand(1e8,1)>0.5)` and a `sum(randi([0 1],1e8,1))` a couple of hundred times and taking the mean shows that, aside from the factor ~2 speed difference, it seems that `randi` also tends more towards the `0`. But the tendency is so small, that if it really bothers someone, that someone should not be using pseudo-random numbers to begin with :p – Rody Oldenhuis Oct 14 '12 at 18:08
5

Try this:

A = rand(50, 10) > 0.5;

The decimal equivalent of the nth row is given by:

 bin2dec(num2str(A(n,:)))

or, if you prefer,

sum( A(n,:) .* 2.^(size(A,2)-1:-1:0) )   % for big endian
sum( A(n,:) .* 2.^(0:size(A,2)-1) )      % for little endian

which is several times faster than bin2dec.

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
5

Another option:

 A=round(rand(50,10));

The decimal eq of the n-th row is given by:

 bin2dec(num2str(A(n,:)))
bla
  • 25,846
  • 10
  • 70
  • 101
2

Through the other answares are shorter I find it rather unappealing generating random numbers with 32 or 64 bit numbers and then throwing away 31 or 63 of them... and rather go with something like:

A_dec=randi([0,2^10-1],50,1,'uint16');

And to get the bits:

A_bin=bsxfun(@(a,i)logical(bitget(a,i)),A_dec,10:-1:1);

This is also several times faster for larger arrays (R2014a, i7 930)[but that doesn't seem to be of importance for the OP]:

tic; for i=1:1000;n = randi([0,2^10-1],50000,1,'uint16'); end;toc

Elapsed time is 1.341566 seconds.

tic; for i=1:1000;n =bsxfun(@(n,i)logical(bitget(n,i)),randi([0,2^10-1],50000,1,'uint16'),10:-1:1); end;toc

Elapsed time is 2.547187 seconds. 

tic; for i=1:1000;n = rand(50000,10)>0.5; end;toc

Elapsed time is 8.030767 seconds.

tic; for i=1:1000;n = sum(bsxfun(@times,rand(50000,10)>0.5,2.^(0:9)),2); end;toc

Elapsed time is 13.062462 seconds.

binornd is even slower:

tic; for i=1:1000;n = logical(binornd(ones(50000,10),0.5)); end;toc

Elapsed time is 47.657960 seconds.

Note that this is still not optimal due to the way MATLAB saves logicals. (bits saved as bytes...)

jan-glx
  • 7,611
  • 2
  • 43
  • 63
  • Interesting idea. But doesn't this assume that the bits themselves are uniformly distributed? Is that true? Why is half of the `A_bin` matrix zeros? – horchler Feb 18 '14 at 00:13
  • Ups...I thought, because my system was little endian bitget would be counting beginning with the most significant bit, obviously it doesn't. - fixed now. – jan-glx Feb 18 '14 at 08:10
  • It does indeed assume that the bits are uniformly distributed which is the case if all the integer numbers form 0 to 2^nBits-1 are uniformly distributed. Not sure how well the ML random number generator generates unbiased pseudo random numbers and I guess the random numbers generated by the other approaches will be more independent but that really depends on the pseudo random number generator. – jan-glx Feb 18 '14 at 08:16
0

Or you could try it like this:

A = binornd(ones(50,10),p);

This way you also have the option to control the probability of occurrence of ones.

Sohrab
  • 1