Is there anyone who can explain me,
why in matlab, and thus in octave, if(a&b) is interpreted like if(a&&b), whereas c=a&b differs from c=a&&b which means that if(a&b) is not the same as c=a&b;if(c)... Very strange. This feature also applies to while but not to until... Who can explain me??? 
On Thu, Sep 14, 2017 at 02:15:16 +0200, Ernst Reissner wrote:
> Is there anyone who can explain me, > > why in matlab, and thus in octave, > > if(a&b) is interpreted like if(a&&b), > > whereas c=a&b differs from c=a&&b > > which means that > > if(a&b) is not the same as c=a&b;if(c)... > > Very strange. > > This feature also applies to while but not to until... > > Who can explain me??? behavior https://www.mathworks.com/matlabcentral/answers/99518isthelogicaloperatorinmatlabashortcircuitoperator This is a "what", not a "why". I don't know who you can ask for an answer to "why", it just is.  mike signature.asc (849 bytes) Download Attachment 
Yes, Mike, I know the what, but not the why and I really cannot figure
out a reason. The reason why I ask is among other things, what about until in octave: Maybe short circuiting should be also for until, as it is for while and if: while (false & error("both")) "hi";end shortcuts and thus no error while do "hi" until(true error("both")); yields and error. This seems a little inconsistent, but on the other hand, ... what sense makes the matlab feature at all?! > On Thu, Sep 14, 2017 at 02:15:16 +0200, Ernst Reissner wrote: >> Is there anyone who can explain me, >> >> why in matlab, and thus in octave, >> >> if(a&b) is interpreted like if(a&&b), >> >> whereas c=a&b differs from c=a&&b >> >> which means that >> >> if(a&b) is not the same as c=a&b;if(c)... >> >> Very strange. >> >> This feature also applies to while but not to until... >> >> Who can explain me??? > Here is an official answer from Mathworks Support that describes the > behavior > > https://www.mathworks.com/matlabcentral/answers/99518isthelogicaloperatorinmatlabashortcircuitoperator > > This is a "what", not a "why". I don't know who you can ask for an > answer to "why", it just is. > 
Administrator

On 09/13/2017 09:25 PM, Ernst Reissner wrote:
> Yes, Mike, I know the what, but not the why and I really cannot figure > out a reason. > The reason why I ask is among other things, what about until in octave: > Maybe short circuiting should be also for until, as it is for while and if: > while (false & error("both")) "hi";end shortcuts and thus no error while > do "hi" until(true error("both")); yields and error. > > This seems a little inconsistent, > but on the other hand, ... what sense makes the matlab feature at all?! It doesn't make much sense. Have you read http://wiki.octave.org/FAQ#Shortcircuit_.26_and_.7C_operators Originally in Octave && was equivalent to &, and  was equivalent to . Neither form did shortcircuiting. I changed the && and  operators (but not the & and  operators) to do shortcircuiting in 1994, apparently. Here is the message I posted then: http://lists.gnu.org/archive/html/helpoctave/199405/msg00012.html When TMW introduced shortcircuit behavior for the & and  operators in Matlab, they thought they were being clever by not introducing new operators, and just making the existing ones do something different in some contexts. To me, this seemed like a really bad choice and I could immediately see cases where behavior would be different depending on whether the expression appeared directly inside and IF/WHILE condition or not. The Matlab behavior is highly unusual and causes trouble in various ways (some documented in the Octave FAQ). We have it for compatibility ONLY. Using this feature is STRONGLY discouraged. I wish to limit the damage, so I have no interest in extending the behavior to other places in the language. Supporting this feature for compatibility is bad enough. jwe 
On 09/14/2017 06:07 AM, John W. Eaton wrote:
> On 09/13/2017 09:25 PM, Ernst Reissner wrote: >> Yes, Mike, I know the what, but not the why and I really cannot figure >> out a reason. >> The reason why I ask is among other things, what about until in octave: >> Maybe short circuiting should be also for until, as it is for while >> and if: >> while (false & error("both")) "hi";end shortcuts and thus no error while >> do "hi" until(true error("both")); yields and error. >> >> This seems a little inconsistent, >> but on the other hand, ... what sense makes the matlab feature at all?! > > It doesn't make much sense. > > Have you read > > http://wiki.octave.org/FAQ#Shortcircuit_.26_and_.7C_operators > > Originally in Octave && was equivalent to &, and  was equivalent to > . Neither form did shortcircuiting. > > I changed the && and  operators (but not the & and  operators) to > do shortcircuiting in 1994, apparently. Here is the message I posted > then: > > http://lists.gnu.org/archive/html/helpoctave/199405/msg00012.html > > When TMW introduced shortcircuit behavior for the & and  operators > in Matlab, they thought they were being clever by not introducing new > operators, and just making the existing ones do something different in > some contexts. To me, this seemed like a really bad choice and I > could immediately see cases where behavior would be different > depending on whether the expression appeared directly inside and > IF/WHILE condition or not. > > The Matlab behavior is highly unusual and causes trouble in various > ways (some documented in the Octave FAQ). We have it for > compatibility ONLY. Using this feature is STRONGLY discouraged. I > wish to limit the damage, so I have no interest in extending the > behavior to other places in the language. Supporting this feature for > compatibility is bad enough. > > jwe > > design choice of TMW. This explains also why the operators  and & in conditions in until statements are not shortcut, i.e. implicitly replaced by && and : because until does not exist in matlab. Maybe good idea to document this.... 
Administrator

On 09/14/2017 05:11 AM, Ernst Reissner wrote:
> This explains also why the operators  and & in conditions in until > statements are not shortcut, > > i.e. implicitly replaced by && and : because until does not exist in > matlab. The shortcircuit behavior of  and & inside IF and WHILE conditions is not identical to the shortcircuit behavior of  and &&. With  and &&, the operands are evaluated and determined whether they are true or false. In Octave, this means that if an operand is an array (for example) it is considered to true if all of the elements are nonzero. In Matlab, I believe it is an error if the operands are not scalar values. In the  and & expressions inside an IF or WHILE condition, short circuiting can only happen if the first argument is a scalar value. It might seem that these are equivalent, at least in Matlab, but consider this case: if (1  x) ...; end vs. cond = 1  x; if (cond) ...; end Are these always equivalent? Unfortunately no, because in the first case, the operator always shortcircuits because the first operand is a scalar and is true, but in the second case, shortcircuiting doesn't happen and if X is an empty matrix, then cond will also be an empty matrix and in the IF condition it is considered false. It seems to me that because of this bizarre behavior, it's best to NEVER use  or & inside IF or WHILE conditions, and only use them as elementwise array operators. Note that using  instead, there is no difference in behavior, so if (1  x) ...; end and cond = 1  x; if (cond) ...; end should always do the same thing, regardless of the value of X. jwe 
Free forum by Nabble  Edit this page 