<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/169986</link>
    <title>MATLAB Central Newsreader - How to vectorize this?</title>
    <description>Feed for thread: How to vectorize this?</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, 27 May 2008 14:31:02 -0400</pubDate>
      <title>How to vectorize this?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/169986#434280</link>
      <author>David Doria</author>
      <description>Frequently I would like to operate on a matrix one row at a&lt;br&gt;
time. I always give up and resort to a for loop in a case&lt;br&gt;
like this.  Can someone explain how you would vectorize this&lt;br&gt;
loop?&lt;br&gt;
&lt;br&gt;
forward = [1 2 3];&lt;br&gt;
location = [4 5 6];&lt;br&gt;
&lt;br&gt;
%Points is a matrix like this:&lt;br&gt;
%x1 y1 z1&lt;br&gt;
%x2 y2 z2&lt;br&gt;
%x3 y3 z3&lt;br&gt;
%etc&lt;br&gt;
&lt;br&gt;
for counter = 1:size(Points,1)&lt;br&gt;
&amp;nbsp;&amp;nbsp;b = Points(counter, :) - location;&lt;br&gt;
&amp;nbsp;&amp;nbsp;projected = dot(forward,b);&lt;br&gt;
&amp;nbsp;&amp;nbsp;L(counter,1) = norm(projected);&lt;br&gt;
&amp;nbsp;&amp;nbsp;L(counter,2) = Points(counter,2);&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
Thanks!&lt;br&gt;
Dave</description>
    </item>
    <item>
      <pubDate>Tue, 27 May 2008 15:52:05 -0400</pubDate>
      <title>Re: How to vectorize this?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/169986#434297</link>
      <author>aasim Azooz</author>
      <description>&quot;David Doria&quot; &amp;lt;daviddoria@gmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;lt;g1h5v5$o85$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Frequently I would like to operate on a matrix one row at &lt;br&gt;
a&lt;br&gt;
&amp;gt; time. I always give up and resort to a for loop in a case&lt;br&gt;
&amp;gt; like this.  Can someone explain how you would vectorize &lt;br&gt;
this&lt;br&gt;
&amp;gt; loop?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; forward = [1 2 3];&lt;br&gt;
&amp;gt; location = [4 5 6];&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; %Points is a matrix like this:&lt;br&gt;
&amp;gt; %x1 y1 z1&lt;br&gt;
&amp;gt; %x2 y2 z2&lt;br&gt;
&amp;gt; %x3 y3 z3&lt;br&gt;
&amp;gt; %etc&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for counter = 1:size(Points,1)&lt;br&gt;
&amp;gt;   b = Points(counter, :) - location;&lt;br&gt;
&amp;gt;   projected = dot(forward,b);&lt;br&gt;
&amp;gt;   L(counter,1) = norm(projected);&lt;br&gt;
&amp;gt;   L(counter,2) = Points(counter,2);&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks!&lt;br&gt;
&amp;gt; Dave&lt;br&gt;
&lt;br&gt;
if x is an nXm matrix then setting &lt;br&gt;
y=x(1,:)&lt;br&gt;
will give you the first raw of x in y&lt;br&gt;
y=x(2,:)&lt;br&gt;
will give you the second raw&lt;br&gt;
stop going back to old good memories of fortran and basic&lt;br&gt;
Aasim Azooz</description>
    </item>
    <item>
      <pubDate>Tue, 27 May 2008 16:14:02 -0400</pubDate>
      <title>Re: How to vectorize this?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/169986#434302</link>
      <author>David Doria</author>
      <description>yes, i've got that part, but I dont want to get each row at&lt;br&gt;
a time in a loop, I'd like to vectorize the loop</description>
    </item>
    <item>
      <pubDate>Tue, 27 May 2008 17:21:01 -0400</pubDate>
      <title>Re: How to vectorize this?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/169986#434315</link>
      <author>Simon Preston</author>
      <description>&quot;David Doria&quot; &amp;lt;daviddoria@gmail.com&amp;gt; wrote in message&lt;br&gt;
&amp;lt;g1h5v5$o85$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Frequently I would like to operate on a matrix one row at a&lt;br&gt;
&amp;gt; time. I always give up and resort to a for loop in a case&lt;br&gt;
&amp;gt; like this.  Can someone explain how you would vectorize this&lt;br&gt;
&amp;gt; loop?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; forward = [1 2 3];&lt;br&gt;
&amp;gt; location = [4 5 6];&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; %Points is a matrix like this:&lt;br&gt;
&amp;gt; %x1 y1 z1&lt;br&gt;
&amp;gt; %x2 y2 z2&lt;br&gt;
&amp;gt; %x3 y3 z3&lt;br&gt;
&amp;gt; %etc&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for counter = 1:size(Points,1)&lt;br&gt;
&amp;gt;   b = Points(counter, :) - location;&lt;br&gt;
&amp;gt;   projected = dot(forward,b);&lt;br&gt;
&amp;gt;   L(counter,1) = norm(projected);&lt;br&gt;
&amp;gt;   L(counter,2) = Points(counter,2);&lt;br&gt;
&amp;gt; end&lt;br&gt;
&lt;br&gt;
Along these lines:&lt;br&gt;
b = Points - repmat(location, [size(Points,1) 1]);&lt;br&gt;
L(:,1) = sum(repmat(forward, [size(Points,1) 1])).*b,2);&lt;br&gt;
L(:,2) = Points(counter,2)&lt;br&gt;
&lt;br&gt;
(Not sure why you find the norm of a the scalar 'projected')&lt;br&gt;
&lt;br&gt;
Best wishes, S</description>
    </item>
    <item>
      <pubDate>Tue, 27 May 2008 18:14:02 -0400</pubDate>
      <title>Re: How to vectorize this?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/169986#434331</link>
      <author>David Doria</author>
      <description>haha clearly you wouldn't take the norm of a scalar, that&lt;br&gt;
was an artifact of me simplifying the code before posting&lt;br&gt;
and not deleting things so it still made sense!&lt;br&gt;
&lt;br&gt;
ok, now the question is, you used a function like &quot;sum&quot;&lt;br&gt;
which can deal with matrices passed as parameters.  If I had&lt;br&gt;
a function that only can operate on once vector at a time&lt;br&gt;
and wanted to make it act like sum does, I would just put a&lt;br&gt;
for loop in it! This has just moved the for loop inside the&lt;br&gt;
function rather than make it behave any differently, right?&lt;br&gt;
&lt;br&gt;
Say the function &lt;br&gt;
function MyCross(A, B)&lt;br&gt;
%take cross product of A and B&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
how would I call this in the original loop I posted instead&lt;br&gt;
of the dot product which you replaced by using the sum()&lt;br&gt;
function?&lt;br&gt;
&lt;br&gt;
Thanks for the help!</description>
    </item>
    <item>
      <pubDate>Tue, 27 May 2008 22:16:01 -0400</pubDate>
      <title>Re: How to vectorize this?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/169986#434398</link>
      <author>Simon Preston</author>
      <description>&quot;David Doria&quot; &amp;lt;daviddoria@gmail.com&amp;gt; wrote in message&lt;br&gt;
&amp;lt;g1hj1a$e43$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; haha clearly you wouldn't take the norm of a scalar, that&lt;br&gt;
&amp;gt; was an artifact of me simplifying the code before posting&lt;br&gt;
&amp;gt; and not deleting things so it still made sense!&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; ok, now the question is, you used a function like &quot;sum&quot;&lt;br&gt;
&amp;gt; which can deal with matrices passed as parameters.  If I had&lt;br&gt;
&amp;gt; a function that only can operate on once vector at a time&lt;br&gt;
&amp;gt; and wanted to make it act like sum does, I would just put a&lt;br&gt;
&amp;gt; for loop in it! This has just moved the for loop inside the&lt;br&gt;
&amp;gt; function rather than make it behave any differently, right?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Say the function &lt;br&gt;
&amp;gt; function MyCross(A, B)&lt;br&gt;
&amp;gt; %take cross product of A and B&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; how would I call this in the original loop I posted instead&lt;br&gt;
&amp;gt; of the dot product which you replaced by using the sum()&lt;br&gt;
&amp;gt; function?&lt;br&gt;
&lt;br&gt;
You're right, moving the loop inside the function will do&lt;br&gt;
nothing to make it run faster.&lt;br&gt;
&lt;br&gt;
There's no one solution to vectorizing code - only way is to&lt;br&gt;
look and learn how others do it.  For your the cross product&lt;br&gt;
example, see how Matlab's vectorized version works:&lt;br&gt;
edit cross&lt;br&gt;
&lt;br&gt;
Best wishes, S</description>
    </item>
  </channel>
</rss>

