<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255318</link>
    <title>MATLAB Central Newsreader - Vectorization</title>
    <description>Feed for thread: Vectorization</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, 04 Jul 2009 20:24:27 -0400</pubDate>
      <title>Vectorization</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255318#662633</link>
      <author>laxxy</author>
      <description>Hi all --&lt;br&gt;
&lt;br&gt;
I am wondering if it is possible to eliminate the loop in the&lt;br&gt;
following piece of code:&lt;br&gt;
&lt;br&gt;
for i=1:K&lt;br&gt;
&amp;nbsp;&amp;nbsp;W(i,:) = Q(i,:)*V(:,P(i,:));&lt;br&gt;
end;&lt;br&gt;
&lt;br&gt;
(that is: each row i of the result is computed by multiplying ith row&lt;br&gt;
of matrix Q by a permutation of columns of V specified by the i'th row&lt;br&gt;
of P).&lt;br&gt;
&lt;br&gt;
Here Q is a KxK square matrix; and all other matrices are KxN. P is an&lt;br&gt;
existing index matrix containing indices into columns of V.&lt;br&gt;
&lt;br&gt;
I've tried a few things but so far have not had too much success... :(</description>
    </item>
    <item>
      <pubDate>Sat, 04 Jul 2009 21:27:00 -0400</pubDate>
      <title>Re: Vectorization</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255318#662638</link>
      <author>Yi Cao</author>
      <description>laxxy &amp;lt;laxyfoxx@gmail.com&amp;gt; wrote in message &amp;lt;8d98157c-ab47-40cb-8e4f-af119371a0db@h8g2000yqm.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi all --&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I am wondering if it is possible to eliminate the loop in the&lt;br&gt;
&amp;gt; following piece of code:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for i=1:K&lt;br&gt;
&amp;gt;   W(i,:) = Q(i,:)*V(:,P(i,:));&lt;br&gt;
&amp;gt; end;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; (that is: each row i of the result is computed by multiplying ith row&lt;br&gt;
&amp;gt; of matrix Q by a permutation of columns of V specified by the i'th row&lt;br&gt;
&amp;gt; of P).&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Here Q is a KxK square matrix; and all other matrices are KxN. P is an&lt;br&gt;
&amp;gt; existing index matrix containing indices into columns of V.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I've tried a few things but so far have not had too much success... :(&lt;br&gt;
&lt;br&gt;
W=Q*V(repmat((1:K)',1,N)+(P-1)*K);&lt;br&gt;
&lt;br&gt;
HTH&lt;br&gt;
Yi</description>
    </item>
    <item>
      <pubDate>Sun, 05 Jul 2009 11:47:11 -0400</pubDate>
      <title>Re: Vectorization</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255318#662690</link>
      <author>laxxy</author>
      <description>&amp;gt; &amp;gt; I am wondering if it is possible to eliminate the loop in the&lt;br&gt;
&amp;gt; &amp;gt; following piece of code:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; for i=1:K&lt;br&gt;
&amp;gt; &amp;gt; &#160; W(i,:) = Q(i,:)*V(:,P(i,:));&lt;br&gt;
&amp;gt; &amp;gt; end;&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; (that is: each row i of the result is computed by multiplying ith row&lt;br&gt;
&amp;gt; &amp;gt; of matrix Q by a permutation of columns of V specified by the i'th row&lt;br&gt;
&amp;gt; &amp;gt; of P).&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; W=Q*V(repmat((1:K)',1,N)+(P-1)*K);&lt;br&gt;
&lt;br&gt;
Thanks!&lt;br&gt;
But this is not quite the same though: in your solution each row of&lt;br&gt;
the V(...) is permuted differently; but the i'th iteration of the loop&lt;br&gt;
computes the same permutation on all rows of V which is specified&lt;br&gt;
given by P(i,:)...</description>
    </item>
    <item>
      <pubDate>Sun, 05 Jul 2009 12:05:03 -0400</pubDate>
      <title>Re: Vectorization</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255318#662696</link>
      <author>Rune Allnor</author>
      <description>On 4 Jul, 22:24, laxxy &amp;lt;laxyf...@gmail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; Hi all --&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; I am wondering if it is possible to eliminate the loop in the&lt;br&gt;
&amp;gt; following piece of code:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; for i=1:K&lt;br&gt;
&amp;gt; &#160; W(i,:) = Q(i,:)*V(:,P(i,:));&lt;br&gt;
&amp;gt; end;&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; (that is: each row i of the result is computed by multiplying ith row&lt;br&gt;
&amp;gt; of matrix Q by a permutation of columns of V specified by the i'th row&lt;br&gt;
&amp;gt; of P).&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Here Q is a KxK square matrix; and all other matrices are KxN. P is an&lt;br&gt;
&amp;gt; existing index matrix containing indices into columns of V.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; I've tried a few things but so far have not had too much success... :(&lt;br&gt;
&lt;br&gt;
What about&lt;br&gt;
&lt;br&gt;
W(1:K,:) = Q(1:K,:)*V(:,P(1:K,:));&lt;br&gt;
&lt;br&gt;
Rune</description>
    </item>
    <item>
      <pubDate>Sun, 05 Jul 2009 12:36:01 -0400</pubDate>
      <title>Re: Vectorization</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255318#662699</link>
      <author>Bruno Luong</author>
      <description>W = Q*V;&lt;br&gt;
W = W(bsxfun(@plus,(1:K)',(P-1)*K))&lt;br&gt;
&lt;br&gt;
% Bruno</description>
    </item>
    <item>
      <pubDate>Sun, 05 Jul 2009 19:07:00 -0400</pubDate>
      <title>Re: Vectorization</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255318#662736</link>
      <author>Yi Cao</author>
      <description>laxxy &amp;lt;laxyfoxx@gmail.com&amp;gt; wrote in message &amp;lt;491153fb-0184-4438-997d-cacf25797dea@i6g2000yqj.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; I am wondering if it is possible to eliminate the loop in the&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; following piece of code:&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; for i=1:K&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; ? W(i,:) = Q(i,:)*V(:,P(i,:));&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; end;&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; (that is: each row i of the result is computed by multiplying ith row&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; of matrix Q by a permutation of columns of V specified by the i'th row&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; of P).&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; W=Q*V(repmat((1:K)',1,N)+(P-1)*K);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks!&lt;br&gt;
&amp;gt; But this is not quite the same though: in your solution each row of&lt;br&gt;
&amp;gt; the V(...) is permuted differently; but the i'th iteration of the loop&lt;br&gt;
&amp;gt; computes the same permutation on all rows of V which is specified&lt;br&gt;
&amp;gt; given by P(i,:)...&lt;br&gt;
&lt;br&gt;
Bruno is right. Instead of permutating before multiplying, it should be after the production. Try the following code:&lt;br&gt;
&lt;br&gt;
% prepare some data&lt;br&gt;
K=10;&lt;br&gt;
N=20;&lt;br&gt;
Q=randn(K);&lt;br&gt;
V=randn(K,N);&lt;br&gt;
[Z,P]=sort(V,2); % produce some permutation&lt;br&gt;
&lt;br&gt;
% vectorized&lt;br&gt;
Z=Q*V;&lt;br&gt;
Z=Z(repmat((1:K)',1,N)+(P-1)*K);&lt;br&gt;
&lt;br&gt;
%original loop&lt;br&gt;
W=zeros(K,N);&lt;br&gt;
for i=1:K&lt;br&gt;
W(i,:) = Q(i,:)*V(:,P(i,:));&lt;br&gt;
end;&lt;br&gt;
&lt;br&gt;
%compare difference&lt;br&gt;
norm(W-Z)&lt;br&gt;
&lt;br&gt;
% should be very small&lt;br&gt;
&lt;br&gt;
HTH&lt;br&gt;
Yi</description>
    </item>
    <item>
      <pubDate>Mon, 06 Jul 2009 02:46:56 -0400</pubDate>
      <title>Re: Vectorization</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255318#662785</link>
      <author>laxxy</author>
      <description>Very cute -- thanks a lot Yi &amp; Bruno!!&lt;br&gt;
&lt;br&gt;
&amp;gt; % vectorized&lt;br&gt;
&amp;gt; Z=Q*V;&lt;br&gt;
&amp;gt; Z=Z(repmat((1:K)',1,N)+(P-1)*K);&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; %original loop&lt;br&gt;
&amp;gt; W=zeros(K,N);&lt;br&gt;
&amp;gt; for i=1:K&lt;br&gt;
&amp;gt; W(i,:) = Q(i,:)*V(:,P(i,:));&lt;br&gt;
&amp;gt; end;</description>
    </item>
  </channel>
</rss>

