Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: Conditional assignment and evaluation (2 issues)

Subject: Conditional assignment and evaluation (2 issues)

From: Dimitri Shvorob

Date: 25 Sep, 2007 09:17:48

Message: 1 of 13

..i.e. the operator that would work like the function below

function[x] = ifn(c,a,b)
if c
   x = a;
else
   x = b;
end

The difficulty with this implementation is that both
expressions have to be valid ('evaluable') whether c is true
or false, a problem if I have something like

x = ifn(i > 0,a(i),b)

For i = 0, I would like to evaluate b only, but 'a(i)' is
evaluated as well and triggers an error.

Can anyone suggest a remedy?

Thank you.




Subject: Conditional assignment and evaluation (2 issues)

From: David

Date: 25 Sep, 2007 09:52:53

Message: 2 of 13

"Dimitri Shvorob" <not.dimitri.shvorob@vanderbilt.edu>
wrote in message <fdajns$568$1@fred.mathworks.com>...
> ..i.e. the operator that would work like the function
below
>
> function[x] = ifn(c,a,b)
> if c
> x = a;
> else
> x = b;
> end
>
> The difficulty with this implementation is that both
> expressions have to be valid ('evaluable') whether c is
true
> or false, a problem if I have something like
>
> x = ifn(i > 0,a(i),b)
>
> For i = 0, I would like to evaluate b only, but 'a(i)' is
> evaluated as well and triggers an error.
>
> Can anyone suggest a remedy?
>
> Thank you.
>
>
>
>

if a(i) is not valid then it can't be passed to the
function. so its not the function that is the problem, it
is the call to the function that is invalid. so the only
fix is to make a(i) something legal.

if this is the only use of the ifn function you could do
something like:

x = ifn(i,a,b)


function[x] = ifn(c,a,b)
if c>0
   x = a(c);
else
   x = b;
end

Subject: Conditional assignment and evaluation (2 issues)

From: Dimitri Shvorob

Date: 25 Sep, 2007 12:24:42

Message: 3 of 13

I did mention that there were two issues involved. A
workaround for one of them is to supply strings to one's IF
function, and have them evaluated on as-needed basis.

function[x] = ifc(c,sa,sb)
if c
   x = evalin('base',sa);
else
   x = evalin('base',sb);
end

But I just couldn't believe that Matlab doesn't have an
operator (? : ) found in a 'general-purpose' language like C
or Java.

Subject: Conditional assignment and evaluation (2 issues)

From: David

Date: 25 Sep, 2007 12:47:18

Message: 4 of 13

"Dimitri Shvorob" <not.dimitri.shvorob@vanderbilt.edu>
wrote in message <fdaum9$boe$1@fred.mathworks.com>...
> I did mention that there were two issues involved. A
> workaround for one of them is to supply strings to one's
IF
> function, and have them evaluated on as-needed basis.
>
> function[x] = ifc(c,sa,sb)
> if c
> x = evalin('base',sa);
> else
> x = evalin('base',sb);
> end
>
> But I just couldn't believe that Matlab doesn't have an
> operator (? : ) found in a 'general-purpose' language
like C
> or Java.

yes, you 'could' do that... but that places lots of
restrictions on ifn. it has to know that sa and sb are in
the base workspace, so they can not be local variables in
another function that calls ifn. my solution essentially
does the same thing, it doesn't evaluate the a(i) in the
function call, it defers it to the case where i has
already been checked to see that it is >0.

just remember, matlab scripting is not a 'general purpose'
language. it is specialized and has lots of things that
are not available in other languages... and of course is
missing a few things, like the ?: operator and a goto
statement. both of which are easily avoided anyway.

Subject: Conditional assignment and evaluation (2 issues)

From: Steven Lord

Date: 25 Sep, 2007 13:36:27

Message: 5 of 13


"Dimitri Shvorob" <not.dimitri.shvorob@vanderbilt.edu> wrote in message
news:fdajns$568$1@fred.mathworks.com...
> ..i.e. the operator that would work like the function below
>
> function[x] = ifn(c,a,b)
> if c
> x = a;
> else
> x = b;
> end
>
> The difficulty with this implementation is that both
> expressions have to be valid ('evaluable') whether c is true
> or false, a problem if I have something like
>
> x = ifn(i > 0,a(i),b)
>
> For i = 0, I would like to evaluate b only, but 'a(i)' is
> evaluated as well and triggers an error.
>
> Can anyone suggest a remedy?

It sounds like you want to do something like:

x = 1:10;
a = @(t) x(t);
b = -1;
c = 0;
if c > 0
  q = a(c);
else
  q = b;
end

or, to make it a little more general:

% begin example1.m
function q = example1(c)
if nargin < 1
  c = 0;
end
x = 1:10;
q = qc(c, @(t) x(t), @(t) -ones(size(t)));

function q = qc(c, a, b)
q = zeros(size(c));
cpos = c > 0;
q(cpos) = a(c(cpos));
q(~cpos) = b(c(~cpos));
% end example1.m

Call this like:

q = example1([1 2 5 0])

--
Steve Lord
slord@mathworks.com


Subject: Conditional assignment and evaluation (2 issues)

From: Dimitri Shvorob

Date: 25 Sep, 2007 21:54:24

Message: 6 of 13

.. I must be missing something - and the uglyish EVAL-based
work-around I have mentioned remains the only way that I've
seen so far to set up a conditional-assignment operator that
would evaluate one, not both, assigned expressions. I do
wish a version of '? :' showed up in a future release of Matlab.


Subject: Conditional assignment and evaluation (2 issues)

From: Loren Shure

Date: 26 Sep, 2007 11:21:52

Message: 7 of 13

In article <fdc02g$8d2$1@fred.mathworks.com>,
not.dimitri.shvorob@vanderbilt.edu says...
> .. I must be missing something - and the uglyish EVAL-based
> work-around I have mentioned remains the only way that I've
> seen so far to set up a conditional-assignment operator that
> would evaluate one, not both, assigned expressions. I do
> wish a version of '? :' showed up in a future release of Matlab.
>
>
>

Steve's post (and quite possibly others!) doesn't require evaluating
both assignments.

--
Loren
http://blogs.mathworks.com/loren/

Subject: Conditional assignment and evaluation (2 issues)

From: Dimitri Shvorob

Date: 26 Sep, 2007 11:56:13

Message: 8 of 13

.. nor does it solve the problem stated in my previous post
- as opposed to the special task of avoiding referencing
zeroth element of an array. (I admit I gave up - with awe
for Steve's finesse - on following the code after 'to make
it more general..').


Subject: Conditional assignment and evaluation (2 issues)

From: Steven Lord

Date: 26 Sep, 2007 18:34:06

Message: 9 of 13


"Dimitri Shvorob" <not.dimitri.shvorob@vanderbilt.edu> wrote in message
news:fddhcs$7n5$1@fred.mathworks.com...
> .. nor does it solve the problem stated in my previous post
> - as opposed to the special task of avoiding referencing
> zeroth element of an array. (I admit I gave up - with awe
> for Steve's finesse - on following the code after 'to make
> it more general..').

I admit I didn't document the example nearly as well as I should have.

The example1 function is simply a demonstration of how to use the qc
(question-colon) function I created. It calls qc with a flag input c (the
expression before the question mark in the ( x ? y : z ) C syntax) that
controls which anonymous function is evaluated. The first anonymous
function is called with those elements of c that are greater than 0 [the
q(cpos) = a(...) line in the qc function] and the second is called with
those that are less than or equal to 0.


% begin example1.m
function q = example1(c)
if nargin < 1
  c = 0;
end
x = 1:10;
q = qc(c, @(t) x(t), @(t) -ones(size(t)));


function q = qc(c, a, b)
% Preallocate the output q to be the same size as c
q = zeros(size(c));

% Determine which elements of c are positive; we'll call the first
% function with those elements as input
cpos = c > 0;

% Call the first function with the positive c's as input
q(cpos) = a(c(cpos));

% Call the second function with the nonpositive c's as input
q(~cpos) = b(c(~cpos));
% end example1.m


There are a couple of issues I can think of that make an operator or
function like ?: trickier to define in MATLAB than in C. I think we can all
agree that if qc(a, b, c) in MATLAB is the same as (a ? b : c) in C, that
these should hold:

x = qc(true, 1, 2) should return 1
x = qc(false, 1, 2) should return 2

But what should these return?

x1 = qc([], 1, 2)
x2 = qc(NaN, 1, 2)
x3 = qc([true, false], 1, 2)
x4 = qc([true, false], magic(3), magic(4))
x5 = qc(struct('a', 1, 'b', 0), 1, 2)
x6 = qc({false}, 1, 2)

I'm curious what type of behavior you would expect from those lines of code.
For instance, I can think of at least three or four possibilities for the x3
case. What do you think they should do?

--
Steve Lord
slord@mathworks.com


Subject: Conditional assignment and evaluation (2 issues)

From: Peter Boettcher

Date: 26 Sep, 2007 20:21:31

Message: 10 of 13

"Steven Lord" <slord@mathworks.com> writes:

> There are a couple of issues I can think of that make an operator or
> function like ?: trickier to define in MATLAB than in C. I think we can all
> agree that if qc(a, b, c) in MATLAB is the same as (a ? b : c) in C, that
> these should hold:
>
> x = qc(true, 1, 2) should return 1
> x = qc(false, 1, 2) should return 2
>
> But what should these return?
>
> x1 = qc([], 1, 2)
> x2 = qc(NaN, 1, 2)
> x3 = qc([true, false], 1, 2)
> x4 = qc([true, false], magic(3), magic(4))
> x5 = qc(struct('a', 1, 'b', 0), 1, 2)
> x6 = qc({false}, 1, 2)
>
> I'm curious what type of behavior you would expect from those lines of code.
> For instance, I can think of at least three or four possibilities for the x3
> case. What do you think they should do?

Easy answer for ALL of the above.

ERROR: First argument must be convertable to a logical scalar

-Peter

Subject: Conditional assignment and evaluation (2 issues)

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 26 Sep, 2007 21:02:43

Message: 11 of 13

In article <muy8x6tb2k4.fsf@G99-Boettcher.llan.ll.mit.edu>,
Peter Boettcher <boettcher@ll.mit.edu> wrote:
>"Steven Lord" <slord@mathworks.com> writes:

>> But what should these return?

>> x1 = qc([], 1, 2)
>> x2 = qc(NaN, 1, 2)
>> x3 = qc([true, false], 1, 2)
>> x4 = qc([true, false], magic(3), magic(4))
>> x5 = qc(struct('a', 1, 'b', 0), 1, 2)
>> x6 = qc({false}, 1, 2)

>Easy answer for ALL of the above.

>ERROR: First argument must be convertable to a logical scalar

[] is convertable to a logical scalar

>> if []; disp('yup'); else; disp('nope'); end
nope

And it is well defined the a vector of logicals is true if all the
elements are true:

>> if [true,false]; disp('yup'); else; disp('nope');end
nope
>> if [true,true]; disp('yup'); else; disp('nope');end
yup

--
   Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson

Subject: Conditional assignment and evaluation (2 issues)

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 26 Sep, 2007 21:15:52

Message: 12 of 13

In article <fde8mv$7l1$1@fred.mathworks.com>,
Steven Lord <slord@mathworks.com> wrote:

>There are a couple of issues I can think of that make an operator or
>function like ?: trickier to define in MATLAB than in C.

Perhaps give the 2nd and third arguments similar semantics to callbacks:
strings are eval'd, function handles are called, cell arrays
that start with a function handle are invoked passing the
remaining arguments. Then as an extension to callback semantics:
anything else is returned as-is. All arguments are evaluated,
so if you want to defer evaluation, use a string or anon function

Thus, qc(x<0,-x,x) would be fine because it doesn't hurt to evaluate both
x and -x

  qc(x<>3, {@(y) (y-3)/(y-3)^2, x}, 1)

When true, the function handle that is the first cell member would
be invoked passing in the second cell member

Or possibly this could even be

  qc(x<>3, @() (x-3)/(x-3)^2, 1)

taking advantage of the "variable capture" effect; this would be
shorthand for

  qc(x<>3, {@() (x-3)/(x-3)^2}, 1)

that is, the handle would be invoked with no arguments.
--
  Is there any thing whereof it may be said, See, this is new? It hath
  been already of old time, which was before us. -- Ecclesiastes

Subject: Conditional assignment and evaluation (2 issues)

From: carlos lopez

Date: 30 Sep, 2007 16:32:18

Message: 13 of 13

Have you considered using try, catch pairs?
Regards
Carlos
"Dimitri Shvorob" <not.dimitri.shvorob@vanderbilt.edu> wrote
in message <fdajns$568$1@fred.mathworks.com>...
> ..i.e. the operator that would work like the function below
>
> function[x] = ifn(c,a,b)
> if c
> x = a;
> else
> x = b;
> end
>
> The difficulty with this implementation is that both
> expressions have to be valid ('evaluable') whether c is true
> or false, a problem if I have something like
>
> x = ifn(i > 0,a(i),b)
>
> For i = 0, I would like to evaluate b only, but 'a(i)' is
> evaluated as well and triggers an error.
>
> Can anyone suggest a remedy?
>
> Thank you.
>
>
>
>

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
if conditional Dimitri Shvorob 25 Sep, 2007 05:20:21
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics