loops - How to make matlab repeat a procedure independently? -
i have matrix this:
a =
1 1 1 0 1 0 1 1 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 1 1
i want replace example 30% of 1 elements in matrix 0 randomly , repeat procedure independently 10 times instance, , @ end of work must have 10 independent matrices each 1 of them should has 30% of 1 elements less original matrix.
here's code use this:
for i=1:10 f=.3; a_ones=find(a); n = round(f*length(a_ones)); a_ones_change = randsample(a_ones,n); a(a_ones_change) = 0; end
but thing matlab code takes original matrix @ begining , replaces 30% of 1 elements 0. second time takes resultant matrix previous step (not original matrix) , replaces 30% of remained 1 elements in matrix 0 , again , again 10 times , @ end gives me 1 matrix below:
a =
0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
how can solve problem , make matlab procedure on 'original matrix a' each i?
your original a
changing every time because of last line in loop a(a_ones_change) = 0;
. change copy of (say, a1 = a
) make in beginning of loop.
Comments
Post a Comment