Can anyone explain to me what is going on in this line of MatLAB code

本秂侑毒 提交于 2019-12-14 04:13:37

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!