Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

& vs. && for MATLAB

  1. Jun 19, 2014 #1


    User Avatar
    Gold Member


    I am having some difficulties understanding the difference between the two and when I would want to use one over the other.
  2. jcsd
  3. Jun 20, 2014 #2


    Staff: Mentor

    in java & represents bitwise anding 000&111 = 000 vs 010&011 = 010 ...

    whereas && represents logical anding where you have two boolean expressions A and B then A&&B will be true only if A is true and B is true.
  4. Jun 20, 2014 #3


    User Avatar
    Gold Member

    In MATLAB, & is the logical elementwise AND operation for matrices.

    && is a short-circuited logical AND operator.

    The short-circuited operators && and || can be quicker than & and | in expressions where the evaluation can end early. For example consider the expression
    Code (Text):

    A && B && C && D
    which evaluates to TRUE only if A, B, C, and D are all TRUE. If A is FALSE, then MATLAB will not even evaluate B, C, or D. However, in the expression
    Code (Text):

    A & B & C & D
    MATLAB will always evaluate A, B, C, and D to determine the result of the expression.
    Last edited: Jun 20, 2014
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook