<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/159578</link>
    <title>MATLAB Central Newsreader - vectorization help</title>
    <description>Feed for thread: vectorization help</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>Tue, 20 Nov 2007 06:15:33 -0500</pubDate>
      <title>vectorization help</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/159578#402199</link>
      <author>KC</author>
      <description>This code takes for ever (well not really, but longer than I think it&lt;br&gt;
should) to run... I can't figure out how to speed it up... any help?&lt;br&gt;
I'm trying to learn vectorization techniques, but I don't see how&lt;br&gt;
vectorization can be applied here.  Can it?&lt;br&gt;
&lt;br&gt;
%% Define angle vectors&lt;br&gt;
x = avgang(:,1);&lt;br&gt;
y = avgang(:,2);&lt;br&gt;
[r,c]=size(angs{1,1});&lt;br&gt;
%% loop&lt;br&gt;
for n = 1:r;&lt;br&gt;
dist(:,n) = sqrt((x-angs{1,1}(n,1)).^2 + (y-angs{1,2}(n,1)).^2);&lt;br&gt;
[minDist(n,1), minDist(n,2)] = min(distVectors(:,n));&lt;br&gt;
%second column is the index number&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
FYI, &quot;angs&quot; is a 1x3 cell (but I only care about cells 1 &amp; 2 for now),&lt;br&gt;
and each cell is a 1001x9 double.  &quot;avgang&quot; is a 1001x3 array, and I&lt;br&gt;
only care about columns 1 &amp; 2 for now.&lt;br&gt;
&lt;br&gt;
Thanks for any help.&lt;br&gt;
-Kieran</description>
    </item>
    <item>
      <pubDate>Tue, 20 Nov 2007 12:14:35 -0500</pubDate>
      <title>Re: vectorization help</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/159578#402234</link>
      <author>Titus</author>
      <description>&lt;br&gt;
&quot;KC&quot; &amp;lt;kc_news@sonic.net&amp;gt; schrieb im Newsbeitrag &lt;br&gt;
news:26726115-2bc1-4fc2-8ce2-7b5a4d1178a1@b40g2000prf.googlegroups.com...&lt;br&gt;
&amp;gt; This code takes for ever (well not really, but longer than I think it&lt;br&gt;
&amp;gt; should) to run... I can't figure out how to speed it up... any help?&lt;br&gt;
&amp;gt; I'm trying to learn vectorization techniques, but I don't see how&lt;br&gt;
&amp;gt; vectorization can be applied here.  Can it?&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; %% Define angle vectors&lt;br&gt;
&amp;gt; x = avgang(:,1);&lt;br&gt;
&amp;gt; y = avgang(:,2);&lt;br&gt;
&amp;gt; [r,c]=size(angs{1,1});&lt;br&gt;
&amp;gt; %% loop&lt;br&gt;
&amp;gt; for n = 1:r;&lt;br&gt;
&amp;gt; dist(:,n) = sqrt((x-angs{1,1}(n,1)).^2 + (y-angs{1,2}(n,1)).^2);&lt;br&gt;
&amp;gt; [minDist(n,1), minDist(n,2)] = min(distVectors(:,n));&lt;br&gt;
&amp;gt; %second column is the index number&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; FYI, &quot;angs&quot; is a 1x3 cell (but I only care about cells 1 &amp; 2 for now),&lt;br&gt;
&amp;gt; and each cell is a 1001x9 double.  &quot;avgang&quot; is a 1001x3 array, and I&lt;br&gt;
&amp;gt; only care about columns 1 &amp; 2 for now.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Thanks for any help.&lt;br&gt;
&amp;gt; -Kieran&lt;br&gt;
&lt;br&gt;
Hi Kieran,&lt;br&gt;
more effectively then thinking too hard about vectorization is &lt;br&gt;
preallocation:&lt;br&gt;
add before the loop:&lt;br&gt;
dist = zeros(length(x), r);&lt;br&gt;
minDist = zeros(r, 2);&lt;br&gt;
This should speed up your code, since MATLAB need not resize the&lt;br&gt;
two matrices in each loop.&lt;br&gt;
&lt;br&gt;
Titus</description>
    </item>
    <item>
      <pubDate>Tue, 20 Nov 2007 17:14:22 -0500</pubDate>
      <title>Re: vectorization help</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/159578#402268</link>
      <author>Pete </author>
      <description>Hi,&lt;br&gt;
I had a quick test and Titus' suggestion of preallocation makes a huge impact on &lt;br&gt;
the length of time it takes and you probably don't need anything else.&lt;br&gt;
&lt;br&gt;
If you really want to vectorize it, I *think* you can take the last bit out of the &lt;br&gt;
loop and have instead&lt;br&gt;
[minDist(:,1), minDist(:,2)] = min(distVectors);&lt;br&gt;
&lt;br&gt;
To vectorize the rest, the only way I can think of would involve creating huge &lt;br&gt;
matrices with repmat and I'm not sure if the advantages would be lost because &lt;br&gt;
of needing more memory and making the code harder to read.&lt;br&gt;
&lt;br&gt;
Instead of your big sqrt line, you could also probably use hypot.&lt;br&gt;
&lt;br&gt;
Though I'm relatively new to MATLAB, so I wouldn't trust anything I suggest &lt;br&gt;
without testing it...&lt;br&gt;
&lt;br&gt;
Pete</description>
    </item>
  </channel>
</rss>

