Effacer les filtres
Effacer les filtres

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 vues (au cours des 30 derniers jours)
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

Réponse acceptée

Walter Roberson
Walter Roberson le 11 Juil 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 commentaires
nafila aytija
nafila aytija le 12 Juil 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 le 12 Juil 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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by