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

2 次查看(过去 30 天)
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

采纳的回答

Walter Roberson
Walter Roberson 2016-7-11
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 个评论
nafila aytija
nafila aytija 2016-7-12
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 2016-7-12
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by