Discussion:
??? Operands to the || and && operators must be convertible to logical scalar values.
(too old to reply)
Paul
2008-05-22 16:45:04 UTC
Permalink
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
2008-05-22 17:20:15 UTC
Permalink
Post by Paul
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.
Post by Paul
??? 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
Paul
2008-05-23 09:32:01 UTC
Permalink
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.
Post by Walter Roberson
Post by Paul
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
Post by Walter Roberson
output argument.
Post by Paul
??? Operands to the || and && operators must be
convertible
Post by Walter Roberson
Post by Paul
to logical scalar values.
quad and other similar functions call the given function
with
Post by Walter Roberson
an array of argument values (different x's), and your
function
Post by Walter Roberson
must return one vector element for each input value. So
your
Post by Walter Roberson
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
Post by Walter Roberson
evaluates to a vector of logical results, so conceptually
you have two logical vectors with the && operator between
them.
Post by Walter Roberson
But && is only defined for scalars (the operands must be
converible to logical *scalar* values), so You Have A
Problem.
Post by Walter Roberson
You might think of substituting the vector "and"
operator, &,
Post by Walter Roberson
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
Post by Walter Roberson
on the right, and a logical operator between them. The
expression
Post by Walter Roberson
after the "if" would then become evaluatable, with a
logical vector
Post by Walter Roberson
as the result. But now we have to consider the question,
"What is the meaning of an 'if' statement whose
controlling expression
Post by Walter Roberson
is a logical vector rather than a scalar?". The result
*is* well defined
Post by Walter Roberson
it turns out: when you have an "if" followed by something
that evaluates
Post by Walter Roberson
to a vector, then the "if" is considered true only if -
all- members
Post by Walter Roberson
of the vector would be considered true. So the above
would be equivilent
Post by Walter Roberson
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
Post by Walter Roberson
functions taking into account the vectorized nature
(e.g., using
Post by Walter Roberson
logical indexing to control which output values are set
to which result);
Post by Walter Roberson
or you can rewrite your functions with an enclosing "for"
loop over
Post by Walter Roberson
all of the x, assigning to out(K) each time (presuming K
is the loop index)
Post by Walter Roberson
rather than just naked out.
--
"A scientist who cannot prove what he has accomplished,
has accomplished nothing." -- Walter
Reisch
Walter Roberson
2008-05-23 20:53:27 UTC
Permalink
Post by Paul
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
2008-05-24 09:17:02 UTC
Permalink
Post by Walter Roberson
Post by Paul
For your interest, I am trying to
translate some existing Mathematica code into Matlab,
and
Post by Walter Roberson
Post by Paul
the ":=" or setDelayed operator in Mathematica is
causing
Post by Walter Roberson
Post by Paul
me some heartache, particularly as I have several
functions
Post by Walter Roberson
Post by Paul
that have this operator nested within them to four or
five
Post by Walter Roberson
Post by Paul
levels!
Oh, just use the extended symbolic toolbox and feed it the
literal Mathematica code (as a string) and ask it to
convert it
Post by Walter Roberson
to the equivilent maple code and run that. You might even
be able
Post by Walter Roberson
to convince it to convert it all to equivilent Matlab
code;
Post by Walter Roberson
the success there would depend upon how heavily the
formulae
Post by Walter Roberson
change form with different input parameters.
I am no stranger to the equivilent of the Mathematica
setDelayed
Post by Walter Roberson
operator: maple has the same thing, and there are some
kinds of
Post by Walter Roberson
programming that it is very valuable for. Sometimes it
gets used to
Post by Walter Roberson
build up complex expressions and functions that are then
evaluated
Post by Walter Roberson
with actual parameters substituted in, but sometimes it
gets used
Post by Walter Roberson
to build up expressions and functions whose number of
terms or structure
Post by Walter Roberson
might depend upon the current value of a parameter, and
the next
Post by Walter Roberson
trip through the loop might end with an expression with a
different
Post by Walter Roberson
number of terms or whatever but built up using the same
meta-methods.
Post by Walter Roberson
It's all the same to maple, but the latter case is a lot
harder to
Post by Walter Roberson
convert into traditional programming.
--
"The slogans of an inadequate criticism peddle ideas to
fashion"
Post by Walter Roberson
-- Walter
Benjamin

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.
Jos
2008-05-22 17:22:02 UTC
Permalink
Post by Paul
function out=fxn1(x)
if x>=0 && x<= 100
out=x;
end
end
??? Operands to the || and && operators must be
convertible
Post by Paul
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
Post by Paul
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?
Look up the difference between && and || versus & and | ...

hth
Jos
Paul
2008-05-23 09:27:01 UTC
Permalink
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.
Post by Paul
Post by Paul
function out=fxn1(x)
if x>=0 && x<= 100
out=x;
end
end
??? Operands to the || and && operators must be
convertible
Post by Paul
to logical scalar values.
The example above is a trivial function, but I have
some
Post by Paul
Post by Paul
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
Post by Paul
m-files, seems convenient. Can anyone suggest how I
might
Post by Paul
Post by Paul
be able to call these functions in quad or other
similar
Post by Paul
Post by Paul
matlab functions, without getting this error message?
Look up the difference between && and || versus & and
| ...
Post by Paul
hth
Jos
Casey Gray
2016-11-01 15:45:03 UTC
Permalink
Post by Paul
function out=fxn1(x)
if x>=0 && x<= 100
out=x;
end
end
??? 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?
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
2017-01-10 07:10:03 UTC
Permalink
Post by Casey Gray
Post by Paul
function out=fxn1(x)
if x>=0 && x<= 100
out=x;
end
end
??? 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?
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. :)
It doesn't always work out. Depends on how complex.
e***@gmail.com
2020-07-18 15:39:45 UTC
Permalink
Post by Casey Gray
Post by Paul
function out=fxn1(x)
if x>=0 && x<= 100
out=x;
end
end
??? 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?
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. :)
Thanks a lot Casey! Really helped.

Loading...