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"
"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"
"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"
"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);
>
> 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
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.