任何人都可以向我解释MatLAB代码这一行中发生了什么 [英] Can anyone explain to me what is going on in this line of MatLAB code
问题描述
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);
我有这个代码. 谁能向我解释这里发生了什么.我试图逐步了解它(调试),但是我不明白在第4行中使用反'〜'以及还将'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.
推荐答案
y = rand(20,3);
创建一个均匀分布的随机数矩阵,y
.
Creates a matrix of uniformly distributed random numbers, y
.
aa= unidrnd(2,20,3) - 1;
创建一个均匀分布的随机整数矩阵,该矩阵从1到2,然后减去1.因此,aa
是0和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);
此行检查aa
为1AND y<1.366e-04
或aa为0 AND y<8.298e-04
的所有值.请注意,这几乎不会发生,因为y
是从0到1的均匀分布的数字,因为它们很小.
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);
取所有以前计算过的情况,并使aa
从0变为1,或者如果在该索引中发生,则从1变为0.
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.
这篇关于任何人都可以向我解释MatLAB代码这一行中发生了什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!