问题
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.
回答1:
y = rand(20,3);
Creates a matrix of uniformly distributed random numbers, y
.
aa= unidrnd(2,20,3) - 1;
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.
val = ( aa & y<1.366e-04) | (~aa & y<8.298e-04);
This line checks all the values where aa
is 1AND y<1.366e-04
OR aa is 0 AND y<8.298e-04
. Note that this barely happens, being y
uniformly distributed numbers from 0 to 1, being them this smalls is unlikely.
aa(val) = ~aa(val);
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.
来源:https://stackoverflow.com/questions/33077249/can-anyone-explain-to-me-what-is-going-on-in-this-line-of-matlab-code