What's the difference between & and && in MATLAB?
Asked Answered
M

7

110

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

Mellisa answered 4/9, 2009 at 13:52 Comment(0)
C
105

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)

Ciborium answered 4/9, 2009 at 14:0 Comment(5)
On caveat: & can operate on arrays but && can only operate on scalars.Fluff
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).Cambogia
@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.Ciborium
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.Ecchymosis
This answer is incomplete and inaccurate. & does short-circuit if in an if statement. And && takes scalar inputs. @Loren's answer below is correct.Backwards
H
39

&& 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.

Hilburn answered 8/9, 2009 at 12:2 Comment(3)
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.Vandal
@Hilburn any idea why they designed one to work with scalars only? It seems strange...Cambogia
@neuronet: You cannot short-circuit if you operate on arrays.Backwards
F
18

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.
Fluff answered 4/9, 2009 at 14:7 Comment(0)
P
9

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.

Pastry answered 4/9, 2009 at 14:1 Comment(0)
B
7

& 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.

Boeotian answered 4/9, 2009 at 14:3 Comment(3)
It's not bitwise, it's element-wise.Backwards
Did I just get well-actually'd 10 years later? 😺Boeotian
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.Backwards
D
4

&& and || are short circuit operators operating on scalars. & and | operate on arrays, and use short-circuiting only in the context of if or while loop expressions.

Dari answered 4/9, 2009 at 14:2 Comment(0)
F
2

A good rule of thumb when constructing arguments for use in conditional statements (IF, WHILE, etc.) is to always use the &&/|| forms, unless there's a very good reason not to. There are two reasons...

  1. As others have mentioned, the short-circuiting behavior of &&/|| is similar to most C-like languages. That similarity / familiarity is generally considered a point in its favor.
  2. Using the && or || forms forces you to write the full code for deciding your intent for vector arguments. When a = [1 0 0 1] and b = [0 1 0 1], is a&b true or false? I can't remember the rules for MATLAB's &, can you? Most people can't. On the other hand, if you use && or ||, you're FORCED to write the code "in full" to resolve the condition.

Doing this, rather than relying on MATLAB's resolution of vectors in & and |, leads to code that's a little bit more verbose, but a LOT safer and easier to maintain.

Fania answered 16/8, 2011 at 5:21 Comment(2)
+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))".Atrice
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!Fania

© 2022 - 2024 — McMap. All rights reserved.