Thread Subject: Properly vectorizing code when using "if"

Subject: Properly vectorizing code when using "if"

From: Johan

Date: 11 Oct, 2008 22:46:02

Message: 1 of 5

Is it possible to "vectorize" an if statement?

For example, in the example below, would like to use y = x^2 when x is less than 0 and y = x^3 when x is greater than zero. Both of these functions are positive in their respective domains. However, the implementation below doesn't work as intended.

Any recommendations?
Thanks.

x = -1:0.01:1;
r = ifTest(x);
plot(x,r);

function[ifTest] = ifTest(x)

if (x < 0 )
   r = x .* x;
else
   r = x .* x .* x;
end
ifTest = r;

Subject: Properly vectorizing code when using "if"

From: Donn Shull

Date: 11 Oct, 2008 23:48:02

Message: 2 of 5

one solution is to use logical operators:

r = ((x>0).*x.*x) + ((x<=0).*x.*x.*x);

Hope this helps,

Donn

"Johan " <robert.mchugh@ips.invensys.com> wrote in message <gcrab9$kns$1@fred.mathworks.com>...
> Is it possible to "vectorize" an if statement?
>
> For example, in the example below, would like to use y = x^2 when x is less than 0 and y = x^3 when x is greater than zero. Both of these functions are positive in their respective domains. However, the implementation below doesn't work as intended.
>
> Any recommendations?
> Thanks.
>
> x = -1:0.01:1;
> r = ifTest(x);
> plot(x,r);
>
> function[ifTest] = ifTest(x)
>
> if (x < 0 )
> r = x .* x;
> else
> r = x .* x .* x;
> end
> ifTest = r;

Subject: Properly vectorizing code when using "if"

From: Lucio Andrade-Cetto

Date: 12 Oct, 2008 00:54:02

Message: 3 of 5

Another a little more elegant

r = x .^ (2+(x<=0));

Lucio

"Donn Shull" <donn.shull.no_spam@aetoolbox.com> wrote in message <gcrdvi$m08$1@fred.mathworks.com>...
> one solution is to use logical operators:
>
> r = ((x>0).*x.*x) + ((x<=0).*x.*x.*x);
>
> Hope this helps,
>
> Donn
>
> "Johan " <robert.mchugh@ips.invensys.com> wrote in message <gcrab9$kns$1@fred.mathworks.com>...
> > Is it possible to "vectorize" an if statement?
> >
> > For example, in the example below, would like to use y = x^2 when x is less than 0 and y = x^3 when x is greater than zero. Both of these functions are positive in their respective domains. However, the implementation below doesn't work as intended.
> >
> > Any recommendations?
> > Thanks.
> >
> > x = -1:0.01:1;
> > r = ifTest(x);
> > plot(x,r);
> >
> > function[ifTest] = ifTest(x)
> >
> > if (x < 0 )
> > r = x .* x;
> > else
> > r = x .* x .* x;
> > end
> > ifTest = r;

Subject: Properly vectorizing code when using "if"

From: Matt Fig

Date: 12 Oct, 2008 00:54:03

Message: 4 of 5

"Johan " <robert.mchugh@ips.invensys.com> wrote in message <gcrab9$kns$1@fred.mathworks.com>...
Both of these functions are positive in their respective domains. However, the implementation below doesn't work as intended.
> x = -1:0.01:1;
> r = ifTest(x);
> plot(x,r);
>



function[x] = ifTest(x)

tmp = x<0;
x(tmp) = x(tmp).^2;
x(~tmp) = x(~tmp).^3

Subject: Properly vectorizing code when using "if"

From: Matt

Date: 12 Oct, 2008 12:59:02

Message: 5 of 5


> if (x < 0 )
> r = x .* x;
> else
> r = x .* x .* x;
> end



Just to explain what went wrong, when you give a vector argument to "if", it tests to see if the real part of all components are non-zero. Otherwise the "else" statements are executed.

So the above code evaluates the "else" statements (on the entire vector x) whenever even one x(i)>0

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
vectors Johan 11 Oct, 2008 18:50:05
rssFeed for this Thread
 

MATLAB Central Terms of Use

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 Terms prior to use.

Contact us at files@mathworks.com