hi,I am trying to generate a vector with zeros and then replacing 20%,30%,40% of the bits with 40.IS it possible?

1 view (last 30 days)
the code vector is v=[0 0 0 0 0 0 0 0 0 0 0 0];
I am trying to generate
v=[0 0 40 0 0 0 40 0 0 0 0]; %%these 40 is random
v=[0 40 0 40 0 0 40 0 0 0];
v=[ 40 0 40 0 0 0 40 0 40 0]
Any kind of suggestion would be appreciated. TIA

Accepted Answer

Walter Roberson
Walter Roberson on 11 Jul 2016
fill_with = 40;
fillpercent = 30;
fill_length = 12;
fill_mask = rand(1, fill_length) * 100 <= fillpercent;
v = zero(1, fill_length);
v(fill_mask) = fill_with;
  2 Comments
nafila aytija
nafila aytija on 12 Jul 2016
thanks a lot for your answer.It works completely fine.But I am trying to do it for a serious of percentage (eg: 30% ,40%,50%) and I do not know how to do it...
Walter Roberson
Walter Roberson on 12 Jul 2016
fillpercents = [30 40 50];
num_percents = length(fillpercents);
v = zeros(num_percents, fill_length);
for K = 1 : num_percents
fill_mask = rand(1, fill_length) * 100 <= fillpercent;
v(K, fill_mask) = fill_with;
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!