Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
& versus &&

Subject: & versus &&

From: John Smith

Date: 11 Aug, 2011 12:16:10

Message: 1 of 3

When I use &, I get the following message:

"Use && instead of & as the AND operator in (scalar) conditional statements."

I wanted to compare two vectors element by element, as well write a conditional statement for just one of the elements of one of the vectors (i.e., solution1(13)>=0 & solution1~=solution2). If I use &&, I can't compare the two vectors as a whole, i.e., element by element. On a simple test I performed, it seemed that & worked fined for writing a conditional statement for just one of the elements.

I hope I'm making sense. My question is, what's wrong with only using &, and never using &&?

Subject: & versus &&

From: Bruno Luong

Date: 11 Aug, 2011 12:30:32

Message: 2 of 3

"John Smith" <johnps@gmail.com> wrote in message <j20h6a$dgg$1@newscl01ah.mathworks.com>...
> When I use &, I get the following message:
>
> "Use && instead of & as the AND operator in (scalar) conditional statements."
>
> I wanted to compare two vectors element by element, as well write a conditional statement for just one of the elements of one of the vectors (i.e., solution1(13)>=0 & solution1~=solution2). If I use &&, I can't compare the two vectors as a whole, i.e., element by element. On a simple test I performed, it seemed that & worked fined for writing a conditional statement for just one of the elements.
>
> I hope I'm making sense. My question is, what's wrong with only using &, and never using &&?

&& is use with boolean scalar expression all skip an evaluation together when it's not needed.

Example, the statement:

b = ~isempty(a) && a(1) > 2;

works for a = [] and as well a = [1 2];

If && is replaced by &, Matlab throws error if a = [];

Bruno

Subject: & versus &&

From: Steven_Lord

Date: 11 Aug, 2011 14:38:16

Message: 3 of 3



"John Smith" <johnps@gmail.com> wrote in message
news:j20h6a$dgg$1@newscl01ah.mathworks.com...
> When I use &, I get the following message:
> "Use && instead of & as the AND operator in (scalar) conditional
> statements."
>
> I wanted to compare two vectors element by element, as well write a
> conditional statement for just one of the elements of one of the vectors
> (i.e., solution1(13)>=0 & solution1~=solution2). If I use &&, I can't
> compare the two vectors as a whole, i.e., element by element. On a simple
> test I performed, it seemed that & worked fined for writing a conditional
> statement for just one of the elements.
> I hope I'm making sense. My question is, what's wrong with only using &,
> and never using &&?

&& short-circuits (doesn't evaluate the second input) if the first input is
false; this can lead to better performance, like:

    y = ~isempty(a) && veryLongComputationInvolvingA(a);

If a is empty, the first input [~isempty(a)] is false. False AND anything is
going to be false regardless of the second input's truth value (or even if
it has a truth value; false && NaN will not error like false & NaN does) so
in this case MATLAB won't need to execute the function
veryLongComputationInvolvingA (which may work if its input is nonempty) and
will simply say y = false.

& does NOT short-circuit except in the context of an IF or WHILE statement
(for backwards compatibility reasons, I believe.) So the following WOULD
execute veryLongComputationInvolvingA even if a is empty.

    y = ~isempty(a) & veryLongComputationInvolvingA(a);

This behavior is documented on the reference page for the logical operators.

http://www.mathworks.com/help/techdoc/ref/logicaloperatorselementwise.html

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us