How to make matlab repeat a procedure independently?
I have a matrix like 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 to replace for example 30% of 1 elements in this matrix with 0
randomly and repeat this procedure independently 10 times for instance,
and at the end of the work I must have 10 independent matrices which each
one of them should has 30% of 1 elements less than the original matrix.
here's the code I use to do 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
A
But the thing that matlab does with this code is that it takes the
original matrix A at the begining and replaces 30% of its 1 elements with
0. But for the second time it takes the resultant matrix from previous
step as A (not the original matrix) and replaces 30% of remained 1
elements in that matrix with 0 and does it again and again for 10 times
and at the end it gives me only 1 matrix like 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 I solve this problem and make matlab to do this procedure on
'original matrix A' for each i?
No comments:
Post a Comment