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

5 Answers 5

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,:)))
Sign up to request clarification or add additional context in comments.

11 Comments

Because ints are ints, and bools are bools. It's always best to stick to type, even in Matlab :)
@RodyOldenhuis OP wanted binary digits, not true and false. That means numbers, not logical values.
Look here for the definition of a "binary digit" :) true and false are simply different words for 1 and 0, respectively.
@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?
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, wheras random ints are done like here. 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.
|
5

Another option:

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

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

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

Comments

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.

Comments

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...)

3 Comments

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?
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.
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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.