y = rand(20,3);
aa= unidrnd(2,20,3) - 1;
val = ( aa & y<1.366e-04) | (~aa & y<8.298e-04);
aa(val) = ~aa(val);
I have this code. Can any one explain to me what is happening here. I have tried to understand it step by step (debugging) but I cannot understand the purpose of using inverse '~' in line 4 and also using 'val' as indices.
Creates a matrix of uniformly distributed random numbers,
y
.Creates a matrix of uniformly distributed random integers, that goes from 1 to 2, and then subtract one. Thus,
aa
is a matrix of 0s and 1s.This line checks all the values where
aa
is 1ANDy<1.366e-04
OR aa is 0 ANDy<8.298e-04
. Note that this barely happens, beingy
uniformly distributed numbers from 0 to 1, being them this smalls is unlikely.Take all those cases computed before, and make
aa
change from 0 to 1 or from 1 to 0 if it happened in that index.