<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/246253</link>
    <title>MATLAB Central Newsreader - Avoiding for loop</title>
    <description>Feed for thread: Avoiding for loop</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>Mon, 09 Mar 2009 22:15:03 -0400</pubDate>
      <title>Avoiding for loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/246253#633618</link>
      <author>Pete sherer</author>
      <description>Are there anyway I can avoid using FOR loop for the case shown below?&lt;br&gt;
&lt;br&gt;
oriX  = [1	2	3	4	5&lt;br&gt;
10	20	30	40	50&lt;br&gt;
100	200	300	400	500&lt;br&gt;
1000	2000	3000	4000	5000];&lt;br&gt;
rowindx = [2 4 1 3 3];&lt;br&gt;
newX= zeros( 1, length(rowindx));&lt;br&gt;
&lt;br&gt;
for ii = 1: 5&lt;br&gt;
newX(ii) = oriX(  rowindx(ii), ii);&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
newX =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;10        2000           3         400         500&lt;br&gt;
&lt;br&gt;
Thanks a lot in advance</description>
    </item>
    <item>
      <pubDate>Mon, 09 Mar 2009 22:23:01 -0400</pubDate>
      <title>Re: Avoiding for loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/246253#633619</link>
      <author>Matt Fig</author>
      <description>[r,c] = size(oriX)&lt;br&gt;
oriX(sub2ind([r,c],rowindx,1:c))&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
alssg`&amp;`n?`&amp;n~nmsfhjunxa~nk9d`g~~~dn`mdk~XhdLDb~ogl~o`tt-`b</description>
    </item>
    <item>
      <pubDate>Tue, 10 Mar 2009 04:47:01 -0400</pubDate>
      <title>Re: Avoiding for loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/246253#633654</link>
      <author>Nasser Abbasi</author>
      <description>&lt;br&gt;
&quot;Pete sherer&quot; &amp;lt;tsh@abg.com&amp;gt; wrote in message &lt;br&gt;
news:gp44d7$bgd$1@fred.mathworks.com...&lt;br&gt;
&amp;gt; Are there anyway I can avoid using FOR loop for the case shown below?&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; oriX  = [1 2 3 4 5&lt;br&gt;
&amp;gt; 10 20 30 40 50&lt;br&gt;
&amp;gt; 100 200 300 400 500&lt;br&gt;
&amp;gt; 1000 2000 3000 4000 5000];&lt;br&gt;
&amp;gt; rowindx = [2 4 1 3 3];&lt;br&gt;
&amp;gt; newX= zeros( 1, length(rowindx));&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; for ii = 1: 5&lt;br&gt;
&amp;gt; newX(ii) = oriX(  rowindx(ii), ii);&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; newX =&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;          10        2000           3         400         500&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Thanks a lot in advance&lt;br&gt;
&lt;br&gt;
Matt just gave you a solution.&lt;br&gt;
&lt;br&gt;
But, I just want to say something here.&lt;br&gt;
&lt;br&gt;
Unless there is a huge performance difference (which might well be the &lt;br&gt;
case), I would prefer to see the loop solution.&lt;br&gt;
&lt;br&gt;
The loop solution is more clear, and someone who is not a matlab expert can &lt;br&gt;
understand it more easily.&lt;br&gt;
&lt;br&gt;
Also, in the future, in the unforeseen event that one wants to 'port' the &lt;br&gt;
code to a different language, it is much easier to convert standard loop &lt;br&gt;
construct as those are standard in many languages.&lt;br&gt;
&lt;br&gt;
All what I am saying is, unless performance is a big issue, stick with the &lt;br&gt;
more easy to understand constructs for the reader, even thought it might &lt;br&gt;
take more key strokes to write it.&lt;br&gt;
&lt;br&gt;
--Nasser </description>
    </item>
    <item>
      <pubDate>Tue, 10 Mar 2009 06:55:03 -0400</pubDate>
      <title>Re: Avoiding for loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/246253#633665</link>
      <author>per isakson</author>
      <description>&quot;Nasser Abbasi&quot; &amp;lt;nma@12000.org&amp;gt; wrote in message &amp;lt;ehmtl.15384$as4.12507@nlpi069.nbdc.sbc.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &quot;Pete sherer&quot; &amp;lt;tsh@abg.com&amp;gt; wrote in message &lt;br&gt;
&amp;gt; news:gp44d7$bgd$1@fred.mathworks.com...&lt;br&gt;
&amp;gt; &amp;gt; Are there anyway I can avoid using FOR loop for the case shown below?&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; oriX  = [1 2 3 4 5&lt;br&gt;
&amp;gt; &amp;gt; 10 20 30 40 50&lt;br&gt;
&amp;gt; &amp;gt; 100 200 300 400 500&lt;br&gt;
&amp;gt; &amp;gt; 1000 2000 3000 4000 5000];&lt;br&gt;
&amp;gt; &amp;gt; rowindx = [2 4 1 3 3];&lt;br&gt;
&amp;gt; &amp;gt; newX= zeros( 1, length(rowindx));&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; for ii = 1: 5&lt;br&gt;
&amp;gt; &amp;gt; newX(ii) = oriX(  rowindx(ii), ii);&lt;br&gt;
&amp;gt; &amp;gt; end&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; newX =&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt;          10        2000           3         400         500&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; Thanks a lot in advance&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Matt just gave you a solution.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; But, I just want to say something here.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Unless there is a huge performance difference (which might well be the &lt;br&gt;
&amp;gt; case), I would prefer to see the loop solution.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The loop solution is more clear, and someone who is not a matlab expert can &lt;br&gt;
&amp;gt; understand it more easily.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Also, in the future, in the unforeseen event that one wants to 'port' the &lt;br&gt;
&amp;gt; code to a different language, it is much easier to convert standard loop &lt;br&gt;
&amp;gt; construct as those are standard in many languages.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; All what I am saying is, unless performance is a big issue, stick with the &lt;br&gt;
&amp;gt; more easy to understand constructs for the reader, even thought it might &lt;br&gt;
&amp;gt; take more key strokes to write it.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; --Nasser &lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
The loop is an order of magnitude fastern than the proposed one-liner on my PC &lt;br&gt;
/per</description>
    </item>
    <item>
      <pubDate>Wed, 11 Mar 2009 03:04:02 -0400</pubDate>
      <title>Re: Avoiding for loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/246253#633940</link>
      <author>Matt Fig</author>
      <description>&quot;per isakson&quot; &amp;lt;poi.nospam@bimDOTkthDOT.se&amp;gt; wrote in message &lt;br&gt;
&amp;gt; The loop is an order of magnitude fastern than the proposed one-liner on my PC &lt;br&gt;
&amp;gt; /per&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
As is so often the case.  In this particular case the for loop is hidden in sub2ind mcode.  So there is the function call overhead and error checking overhead, etc.&lt;br&gt;
&lt;br&gt;
I wonder how many releases of Matlab it will take until there are not so many requests to get rid of the 'evil' For loops.  It seems to me that there is still a lingering perception that For loops in Matlab MUST be slow.  This, of course was understandable prior to 6.5.  Since then, maybe not so much...&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
GHRGA4HA5:RC@&amp;lt;R?6,4RB84B5B@Y8C4L? 8YIG;BR;w;844l&amp;lt;RRRBB4&amp;gt;`r6</description>
    </item>
  </channel>
</rss>

