2012-12-03 54 views
0

在該表達式中Matlab和和<操作者

lmin=lminflag & ~kmod & actminsub<nsm*pminu & actminsub>pminu; 

是&操作者等的逐位AND運算? lminflag和kmod都是具有邏輯1或0作爲元素的數組,lmin結果也是1或0。

回答

5

是的。

  1. &是一個per-element AND運算符。
  2. &&是一個標量AND運算符,有條件執行語句的其餘部分。

例如,給定:

a = true; 
b = false; 
aa = [true false]; 
bb = [true true]; 
fnA = @()rand>0.5; %An anonymous function returning true half the time 

然後:

a & b; %returns false 
a && b; %returns false (same as above) 

然而

aa & bb; %this an error  
aa && bb; %returns the array [true false] 

它更有趣,當操作數功能,具有副作用。

b & fnA; %Returns false, and the `rand` function is called (including a small performance hit, and an update to the random state) 
b && fnA; %Returns false, and the `rand` function was not called (since `b` is false, the actual value of `fnA` doesn;t effect the result 
+0

+1。儘管小的評論:請注意'rand> 0.5;'至少是邏輯(round(rand))的兩倍;' –

+0

真,改變了。值得讓小事情正確。 – Pursuit