Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

??? Operands to the || and && operators must be convertible to logical scalar values.

2,208 views
Skip to first unread message

Paul

unread,
May 22, 2008, 12:45:04 PM5/22/08
to
I have defined the following function m-file:

function out=fxn1(x)

if x>=0 && x<= 100
out=x;
end
end


If I call it using a handle in quad, as follows:

quad(@fxn1,0,10)

I get the error message:

??? Operands to the || and && operators must be convertible
to logical scalar values.

The example above is a trivial function, but I have some
very complex functions, which take different algebraic
forms for different ranges of their arguments. Defining
them as function m-files, using relational operators in the
m-files, seems convenient. Can anyone suggest how I might
be able to call these functions in quad or other similar
matlab functions, without getting this error message?

Walter Roberson

unread,
May 22, 2008, 1:20:15 PM5/22/08
to
In article <g147ug$phn$1...@fred.mathworks.com>,

Paul <paul.e...@nanion.co.uk> wrote:
>I have defined the following function m-file:

>function out=fxn1(x)

>if x>=0 && x<= 100
>out=x;
>end
>end

In the case that x is not in that range, you don't assign the
output argument.


>If I call it using a handle in quad, as follows:

>quad(@fxn1,0,10)

>I get the error message:

>??? Operands to the || and && operators must be convertible
>to logical scalar values.

quad and other similar functions call the given function with
an array of argument values (different x's), and your function
must return one vector element for each input value. So your
statement

if x>=0 && x<= 100

is being applied with x a vector of values. The x>=0
evaluates to a vector of logical results, and the x<= 100 part
evaluates to a vector of logical results, so conceptually
you have two logical vectors with the && operator between them.
But && is only defined for scalars (the operands must be
converible to logical *scalar* values), so You Have A Problem.

You might think of substituting the vector "and" operator, &,
in place of the no-vector &&, something like,

if x>=0 & x<= 100

Now you would have a logical vector on the left, a logical vector
on the right, and a logical operator between them. The expression
after the "if" would then become evaluatable, with a logical vector
as the result. But now we have to consider the question,
"What is the meaning of an 'if' statement whose controlling expression
is a logical vector rather than a scalar?". The result *is* well defined
it turns out: when you have an "if" followed by something that evaluates
to a vector, then the "if" is considered true only if -all- members
of the vector would be considered true. So the above would be equivilent
to

if all(x>=0 & x<= 100)

Chances are that isn't exactly what you want, though.

So, this leaves you with two basic approaches: you can rewrite your
functions taking into account the vectorized nature (e.g., using
logical indexing to control which output values are set to which result);
or you can rewrite your functions with an enclosing "for" loop over
all of the x, assigning to out(K) each time (presuming K is the loop index)
rather than just naked out.
--
"A scientist who cannot prove what he has accomplished,
has accomplished nothing." -- Walter Reisch

Jos

unread,
May 22, 2008, 1:22:02 PM5/22/08
to
"Paul " <paul.e...@nanion.co.uk> wrote in message
<g147ug$phn$1...@fred.mathworks.com>...


Look up the difference between && and || versus & and | ...

hth
Jos

Paul

unread,
May 23, 2008, 5:27:01 AM5/23/08
to
Thanks Jos. I should have pointed out that I had already
tried & versus &&, without luck. The example code I gave
was very trivial, my real problem is too complex to cut and
paste.

"Jos " <DEL...@jasenDEL.nl> wrote in message
<g14a3q$m6d$1...@fred.mathworks.com>...

Paul

unread,
May 23, 2008, 5:32:01 AM5/23/08
to
Thanks Walter, this does make some sense now, although I
think I am going to have to get help to implement your
suggestions - my Matlab skills are limited! I did not
understand the way quad calls the function, but your
explanation is clear. For your interest, I am trying to
translate some existing Mathematica code into Matlab, and
the ":=" or setDelayed operator in Mathematica is causing
me some heartache, particularly as I have several functions
that have this operator nested within them to four or five
levels!

Thanks again for your help.

robe...@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <g14a0f$jcl$1...@canopus.cc.umanitoba.ca>...

Walter Roberson

unread,
May 23, 2008, 4:53:27 PM5/23/08
to
In article <g162uh$a31$1...@fred.mathworks.com>,

Paul <paul.e...@nanion.co.uk> wrote:
>For your interest, I am trying to
>translate some existing Mathematica code into Matlab, and
>the ":=" or setDelayed operator in Mathematica is causing
>me some heartache, particularly as I have several functions
>that have this operator nested within them to four or five
>levels!

Oh, just use the extended symbolic toolbox and feed it the
literal Mathematica code (as a string) and ask it to convert it
to the equivilent maple code and run that. You might even be able
to convince it to convert it all to equivilent Matlab code;
the success there would depend upon how heavily the formulae
change form with different input parameters.

I am no stranger to the equivilent of the Mathematica setDelayed
operator: maple has the same thing, and there are some kinds of
programming that it is very valuable for. Sometimes it gets used to
build up complex expressions and functions that are then evaluated
with actual parameters substituted in, but sometimes it gets used
to build up expressions and functions whose number of terms or structure
might depend upon the current value of a parameter, and the next
trip through the loop might end with an expression with a different
number of terms or whatever but built up using the same meta-methods.
It's all the same to maple, but the latter case is a lot harder to
convert into traditional programming.
--
"The slogans of an inadequate criticism peddle ideas to fashion"
-- Walter Benjamin

Paul

unread,
May 24, 2008, 5:17:02 AM5/24/08
to
robe...@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <g17as7$aie$1...@canopus.cc.umanitoba.ca>...

Hi Walter,

I haven't tried the extended symbolic toolbox, but I've
been told that it probably won't help with our code. We
specifically need Matlab code, and our functions do change
substantially dependng on argument values. I might take a
look at the extended symbolic toolbox if I can't make
progress longhand, but your earlier suggestion seems to be
working fine for now - for loops in the m-file handle the
vector input and output with no problems.

Thanks again.

Casey Gray

unread,
Nov 1, 2016, 11:45:09 AM11/1/16
to
"Paul" wrote in message <g147ug$phn$1...@fred.mathworks.com>...
I know this is old. For anyone searching for a solution to this error....
if x>=0
if x<= 100
out=x;
end
end

Nested if statements will accomplish the goal. :)

Princely

unread,
Jan 10, 2017, 2:10:07 AM1/10/17
to
"Casey Gray" wrote in message <nvad9v$bs0$1...@newscl01ah.mathworks.com>...
It doesn't always work out. Depends on how complex.

email.pra...@gmail.com

unread,
Jul 18, 2020, 11:39:55 AM7/18/20
to
Thanks a lot Casey! Really helped.
0 new messages