Filter löschen
Filter löschen

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 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 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 Kommentare
nafila aytija
nafila aytija am 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 am 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by