What's the difference between & and && in MATLAB?

163,551

Solution 1

The single ampersand & is the logical AND operator. The double ampersand && is again a logical AND operator that employs short-circuiting behaviour. Short-circuiting just means the second operand (right hand side) is evaluated only when the result is not fully determined by the first operand (left hand side)

A & B (A and B are evaluated)

A && B (B is only evaluated if A is true)

Solution 2

&& and || take scalar inputs and short-circuit always. | and & take array inputs and short-circuit only in if/while statements. For assignment, the latter do not short-circuit.

See these doc pages for more information.

Solution 3

As already mentioned by others, & is a logical AND operator and && is a short-circuit AND operator. They differ in how the operands are evaluated as well as whether or not they operate on arrays or scalars:

  • & (AND operator) and | (OR operator) can operate on arrays in an element-wise fashion.
  • && and || are short-circuit versions for which the second operand is evaluated only when the result is not fully determined by the first operand. These can only operate on scalars, not arrays.

Solution 4

Both are logical AND operations. The && though, is a "short-circuit" operator. From the MATLAB docs:

They are short-circuit operators in that they evaluate their second operand only when the result is not fully determined by the first operand.

See more here.

Solution 5

& is a logical elementwise operator, while && is a logical short-circuiting operator (which can only operate on scalars).

For example (pardon my syntax).

If..

A = [True True False True]
B = False
A & B = [False False False False]

..or..

B = True
A & B = [True True False True]

For &&, the right operand is only calculated if the left operand is true, and the result is a single boolean value.

x = (b ~= 0) && (a/b > 18.5)

Hope that's clear.

Share:
163,551

Related videos on Youtube

Fantomas
Author by

Fantomas

Updated on October 02, 2020

Comments

  • Fantomas
    Fantomas over 3 years

    What is the difference between the & and && logical operators in MATLAB?

  • gnovice
    gnovice over 14 years
    On caveat: & can operate on arrays but && can only operate on scalars.
  • Jonas Heidelberg
    Jonas Heidelberg over 12 years
    +1, but it should be noted that your answer only applies to cases where you want the final result of the operation to be scalar. There are many uses for & and | where && and || are useless because they can't return arrays, for example when doing fancy indexing like "selecting all r between 1 and 2: r((r<2)&(r<2))".
  • Bob Gilmore
    Bob Gilmore over 12 years
    Good point, Jonas. I was thinking of conditionals, not "logical indexing," (the MATLAB term for the "fancy indexing" you mentioned) when I wrote this. I changed the first sentence of my post to reflect that. Thanks for the reminder!
  • Tim
    Tim over 9 years
    Do you have any information on which Matlab versions shortcut & and | in if/while statements? It does not seem to be the case in R2012b and R2014a.
  • eric
    eric about 9 years
    @Loren any idea why they designed one to work with scalars only? It seems strange...
  • eric
    eric over 7 years
    Side note: after 15 years working with Matlab almost daily I always use '&' and it has never bitten me in the ass. OTOH, I know many people who get annoyed using '&&' because they have to remember it isn't universal (yes I realize that '&' isn't as efficient because it doesn't short circuit but I pretty much never daisy-chain my operands so the savings nowadays are negligible).
  • Fraser
    Fraser over 7 years
    @neuronet it isn't really about efficiency, more that it permits a construct where the first expression guarantees a condition without which the second expression may cause a run-time error. e.g. d != 0 && 1/d vs d !=0 & 1/d - the first guarantees no division by zero, the second doesn't.
  • Aaron Rotenberg
    Aaron Rotenberg over 5 years
    Be warned! & and | do short-circuit, sometimes. Quoth the documentation: "When you use the element-wise & and | operators in the context of an if or while loop expression (and only in that context), they use short-circuiting to evaluate expressions." This bizarre behavior is peculiar to MATLAB and is not shared by any other language that uses these operators.
  • Cris Luengo
    Cris Luengo about 5 years
    This answer is incomplete and inaccurate. & does short-circuit if in an if statement. And && takes scalar inputs. @Loren's answer below is correct.
  • Cris Luengo
    Cris Luengo about 5 years
    It's not bitwise, it's element-wise.
  • Andras Deak -- Слава Україні
    Andras Deak -- Слава Україні about 5 years
    Also note that Loren is a MathWorks employee. An answer doesn't get more authoritative than that. If you're willing to make a sacrifice you could flag your answer for a mod to delete it (you can't delete it yourself, because it's accepted). You would keep the rep you gained from it (if I understand the system correctly), and we'd end up with a technically correct and authoritative top answer.
  • Erik Kerber
    Erik Kerber about 5 years
    Did I just get well-actually'd 10 years later? 😺
  • Cris Luengo
    Cris Luengo about 5 years
    Well, this question has gotten ~115k views so far, which means a lot of people have read misinformation here. Many of these answers are incomplete or contain wrong information. All you need to do is fix your answer or delete it. BTW: bitand is the bitwise logical AND operator in MATLAB.
  • Cris Luengo
    Cris Luengo about 5 years
    @neuronet: You cannot short-circuit if you operate on arrays.