Generating n bit random numbers with each bit sampled N times.

I want to generate a random sequence of n bits, and each bit is to be sampled by 100 samples. Although i have generated the n bit sequence, how should i sample each bit.
bin_seq = randi([0 1],1,num_bit);
This is what i wrote for generating the n bit sequence where num_bit is the bumber of bits.(20 in my case)

2 Comments

What does this mean: "each bit is to be sampled by 100 samples"?
Do you want to repeat each value 100 times? Then use repelem.
Yes that was what i wanted. Didnt know about the 'repelem' command. Thanks a lot!!

Sign in to comment.

 Accepted Answer

bin_seq = randi([0 1], 1, num_bit);
result = repelem(bin_seq, 1, 100);
Alternatively:
result = reshape(repmat(bin_seq, 100, 1), 1, []);

More Answers (1)

n=8; % number of bits
r=10; % number of bit repetitions, 100 in your case
m=20; % number of sequenes (>=r)
i=zeros(r,n);
for k=1:n
i(:,k) = randperm(m,r);
end
j=repmat(1:n,[r,1]);
b=accumarray([i(:),j(:)],1,[m n]);
disp(b)
1 0 0 1 1 1 1 1 1 1 0 1 0 0 0 0 0 1 1 0 1 1 1 1 0 1 0 1 1 0 0 0 1 0 0 0 0 0 1 0 1 0 1 0 0 0 0 1 1 1 1 1 0 1 0 0 0 0 1 1 1 0 1 1 0 1 1 0 1 1 1 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 1 1 1 0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 1 0 0 1 1 1 1 1 0 0 0 1 1 1 1 0 1 1 0 0 1 0 1 0 1 1 1 1 0 0 0 1 0 1 1 1 1 0 0 1 1 1 0 0
sum(b,1)
ans = 1×8
10 10 10 10 10 10 10 10

Asked:

on 17 Feb 2022

Edited:

on 18 Feb 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!