<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/237385</link>
    <title>MATLAB Central Newsreader - Properly vectorizing code when using &quot;if&quot;</title>
    <description>Feed for thread: Properly vectorizing code when using &quot;if&quot;</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Sat, 11 Oct 2008 22:46:02 -0400</pubDate>
      <title>Properly vectorizing code when using &quot;if&quot;</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/237385#604802</link>
      <author>Johan </author>
      <description>Is it possible to &quot;vectorize&quot; an if statement?&lt;br&gt;
&lt;br&gt;
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.  &lt;br&gt;
&lt;br&gt;
Any recommendations? &lt;br&gt;
Thanks. &lt;br&gt;
&lt;br&gt;
x = -1:0.01:1;&lt;br&gt;
r = ifTest(x);&lt;br&gt;
plot(x,r);&lt;br&gt;
&lt;br&gt;
function[ifTest] = ifTest(x)&lt;br&gt;
&lt;br&gt;
if (x &amp;lt; 0 )&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;r = x .* x;&lt;br&gt;
else&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;r = x .* x .* x;&lt;br&gt;
end&lt;br&gt;
ifTest = r; </description>
    </item>
    <item>
      <pubDate>Sat, 11 Oct 2008 23:48:02 -0400</pubDate>
      <title>Re: Properly vectorizing code when using &quot;if&quot;</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/237385#604804</link>
      <author>Donn Shull</author>
      <description>one solution is to use logical operators:&lt;br&gt;
&lt;br&gt;
r = ((x&amp;gt;0).*x.*x) + ((x&amp;lt;=0).*x.*x.*x);&lt;br&gt;
&lt;br&gt;
Hope this helps,&lt;br&gt;
&lt;br&gt;
Donn&lt;br&gt;
&lt;br&gt;
&quot;Johan &quot; &amp;lt;robert.mchugh@ips.invensys.com&amp;gt; wrote in message &amp;lt;gcrab9$kns$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Is it possible to &quot;vectorize&quot; an if statement?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 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.  &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Any recommendations? &lt;br&gt;
&amp;gt; Thanks. &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; x = -1:0.01:1;&lt;br&gt;
&amp;gt; r = ifTest(x);&lt;br&gt;
&amp;gt; plot(x,r);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; function[ifTest] = ifTest(x)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; if (x &amp;lt; 0 )&lt;br&gt;
&amp;gt;    r = x .* x;&lt;br&gt;
&amp;gt; else&lt;br&gt;
&amp;gt;    r = x .* x .* x;&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; ifTest = r; </description>
    </item>
    <item>
      <pubDate>Sun, 12 Oct 2008 00:54:02 -0400</pubDate>
      <title>Re: Properly vectorizing code when using &quot;if&quot;</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/237385#604811</link>
      <author>Lucio Andrade-Cetto</author>
      <description>Another a little more elegant&lt;br&gt;
&lt;br&gt;
r = x .^ (2+(x&amp;lt;=0));&lt;br&gt;
&lt;br&gt;
Lucio&lt;br&gt;
&lt;br&gt;
&quot;Donn Shull&quot; &amp;lt;donn.shull.no_spam@aetoolbox.com&amp;gt; wrote in message &amp;lt;gcrdvi$m08$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; one solution is to use logical operators:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; r = ((x&amp;gt;0).*x.*x) + ((x&amp;lt;=0).*x.*x.*x);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Hope this helps,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Donn&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &quot;Johan &quot; &amp;lt;robert.mchugh@ips.invensys.com&amp;gt; wrote in message &amp;lt;gcrab9$kns$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; Is it possible to &quot;vectorize&quot; an if statement?&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; 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.  &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Any recommendations? &lt;br&gt;
&amp;gt; &amp;gt; Thanks. &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; x = -1:0.01:1;&lt;br&gt;
&amp;gt; &amp;gt; r = ifTest(x);&lt;br&gt;
&amp;gt; &amp;gt; plot(x,r);&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; function[ifTest] = ifTest(x)&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; if (x &amp;lt; 0 )&lt;br&gt;
&amp;gt; &amp;gt;    r = x .* x;&lt;br&gt;
&amp;gt; &amp;gt; else&lt;br&gt;
&amp;gt; &amp;gt;    r = x .* x .* x;&lt;br&gt;
&amp;gt; &amp;gt; end&lt;br&gt;
&amp;gt; &amp;gt; ifTest = r; </description>
    </item>
    <item>
      <pubDate>Sun, 12 Oct 2008 00:54:03 -0400</pubDate>
      <title>Re: Properly vectorizing code when using &quot;if&quot;</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/237385#604812</link>
      <author>Matt Fig</author>
      <description>&quot;Johan &quot; &amp;lt;robert.mchugh@ips.invensys.com&amp;gt; wrote in message &amp;lt;gcrab9$kns$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
Both of these functions are positive in their respective domains. However, the implementation below doesn't work as intended.   &lt;br&gt;
&amp;gt; x = -1:0.01:1;&lt;br&gt;
&amp;gt; r = ifTest(x);&lt;br&gt;
&amp;gt; plot(x,r);&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
function[x] = ifTest(x)&lt;br&gt;
&lt;br&gt;
tmp = x&amp;lt;0;&lt;br&gt;
x(tmp) = x(tmp).^2;&lt;br&gt;
x(~tmp) = x(~tmp).^3</description>
    </item>
    <item>
      <pubDate>Sun, 12 Oct 2008 12:59:02 -0400</pubDate>
      <title>Re: Properly vectorizing code when using &quot;if&quot;</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/237385#604839</link>
      <author>Matt </author>
      <description>&lt;br&gt;
&amp;gt; if (x &amp;lt; 0 )&lt;br&gt;
&amp;gt;    r = x .* x;&lt;br&gt;
&amp;gt; else&lt;br&gt;
&amp;gt;    r = x .* x .* x;&lt;br&gt;
&amp;gt; end&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Just to explain what went wrong, when you give a vector argument to &quot;if&quot;, it tests to see if the real part of all components are non-zero. Otherwise the &quot;else&quot; statements are executed.&lt;br&gt;
&lt;br&gt;
So the above code evaluates the &quot;else&quot; statements (on the entire vector x) whenever even one x(i)&amp;gt;0 </description>
    </item>
  </channel>
</rss>

