<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239269</link>
    <title>MATLAB Central Newsreader - How to do vectorize this simple code?</title>
    <description>Feed for thread: How to do vectorize this simple code?</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>Fri, 14 Nov 2008 22:09:03 -0500</pubDate>
      <title>How to do vectorize this simple code?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239269#610989</link>
      <author>Henry </author>
      <description>I want to vectorize this simple snippet:&lt;br&gt;
&lt;br&gt;
for i=1:num_elements&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;y(i)=my_matrix(row(i),col(i));&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
I tried y=my_matrix(row,col)&lt;br&gt;
and y=my_matrix([row col])&lt;br&gt;
&lt;br&gt;
but nothing seems to work.  Any pointers?  Thanks.</description>
    </item>
    <item>
      <pubDate>Fri, 14 Nov 2008 22:15:07 -0500</pubDate>
      <title>Re: How to do vectorize this simple code?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239269#610992</link>
      <author>Kenneth Eaton</author>
      <description>&quot;Henry &quot; &amp;lt;henry@fullspectrumengineering.com&amp;gt; wrote in message &amp;lt;gfkstu$1id$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I want to vectorize this simple snippet:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for i=1:num_elements&lt;br&gt;
&amp;gt;     y(i)=my_matrix(row(i),col(i));&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I tried y=my_matrix(row,col)&lt;br&gt;
&amp;gt; and y=my_matrix([row col])&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; but nothing seems to work.  Any pointers?  Thanks.&lt;br&gt;
&lt;br&gt;
Check out the SUB2IND function.&lt;br&gt;
&lt;br&gt;
hth,&lt;br&gt;
Ken</description>
    </item>
    <item>
      <pubDate>Fri, 14 Nov 2008 22:18:01 -0500</pubDate>
      <title>Re: How to do vectorize this simple code?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239269#610993</link>
      <author>Scott </author>
      <description>If I understand you correctly, all you need is the diagonal. I believe this is the simplest way (at least that I'm aware of).&lt;br&gt;
&lt;br&gt;
y(i)=diag(my_matrix(row(i),col(i)));</description>
    </item>
    <item>
      <pubDate>Fri, 14 Nov 2008 22:35:03 -0500</pubDate>
      <title>Re: How to do vectorize this simple code?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239269#610997</link>
      <author>John D'Errico</author>
      <description>&quot;Scott &quot; &amp;lt;crazyivan84__remove__@hotmail.com&amp;gt; wrote in message &amp;lt;gfktep$9al$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; If I understand you correctly, all you need is the diagonal. I believe this is the simplest way (at least that I'm aware of).&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; y(i)=diag(my_matrix(row(i),col(i)));&lt;br&gt;
&lt;br&gt;
This is an extremely inefficient way to do it of course.&lt;br&gt;
&lt;br&gt;
John</description>
    </item>
    <item>
      <pubDate>Fri, 14 Nov 2008 23:17:03 -0500</pubDate>
      <title>Re: How to do vectorize this simple code?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239269#611001</link>
      <author>Mahdieh </author>
      <description>&quot;Henry &quot; &amp;lt;henry@fullspectrumengineering.com&amp;gt; wrote in message &amp;lt;gfkstu$1id$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I want to vectorize this simple snippet:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for i=1:num_elements&lt;br&gt;
&amp;gt;     y(i)=my_matrix(row(i),col(i));&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I tried y=my_matrix(row,col)&lt;br&gt;
&amp;gt; and y=my_matrix([row col])&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; but nothing seems to work.  Any pointers?  Thanks.&lt;br&gt;
&lt;br&gt;
Hi Henry&lt;br&gt;
&lt;br&gt;
I guess this works:&lt;br&gt;
&lt;br&gt;
U = mymatrix(row,:);&lt;br&gt;
V = U(:,col);&lt;br&gt;
y = diag(V);&lt;br&gt;
&lt;br&gt;
-Mah</description>
    </item>
    <item>
      <pubDate>Fri, 14 Nov 2008 23:36:02 -0500</pubDate>
      <title>Re: How to do vectorize this simple code?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239269#611003</link>
      <author>Henry </author>
      <description>Hmm all those suggestions don't seem to be very efficient compared to my original code of:&lt;br&gt;
for i=1:num_elements&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;y(i)=my_matrix(row(i),col(i));&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
I just thought I could write it as a single line like y=my_matrix(row,col) where row and col are vectors and it would match them up as pairs and return a vector.&lt;br&gt;
&lt;br&gt;
Seems like a common procedure and I'm surprised matlab doesn't have it built in...&lt;br&gt;
&lt;br&gt;
&quot;Mahdieh&quot; &amp;lt;mahdieh.emrani@capitalhealth.ca&amp;gt; wrote in message &amp;lt;gfl0tf$srs$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Henry &quot; &amp;lt;henry@fullspectrumengineering.com&amp;gt; wrote in message &amp;lt;gfkstu$1id$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; I want to vectorize this simple snippet:&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; for i=1:num_elements&lt;br&gt;
&amp;gt; &amp;gt;     y(i)=my_matrix(row(i),col(i));&lt;br&gt;
&amp;gt; &amp;gt; end&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; I tried y=my_matrix(row,col)&lt;br&gt;
&amp;gt; &amp;gt; and y=my_matrix([row col])&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; but nothing seems to work.  Any pointers?  Thanks.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Hi Henry&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I guess this works:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; U = mymatrix(row,:);&lt;br&gt;
&amp;gt; V = U(:,col);&lt;br&gt;
&amp;gt; y = diag(V);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; -Mah</description>
    </item>
    <item>
      <pubDate>Fri, 14 Nov 2008 23:43:01 -0500</pubDate>
      <title>Re: How to do vectorize this simple code?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239269#611004</link>
      <author>Scott </author>
      <description>&quot;John D'Errico&quot; &amp;lt;woodchips@rochester.rr.com&amp;gt; wrote in message &amp;lt;gfkuen$m7v$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Scott &quot; &amp;lt;crazyivan84__remove__@hotmail.com&amp;gt; wrote in message &amp;lt;gfktep$9al$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; If I understand you correctly, all you need is the diagonal. I believe this is the simplest way (at least that I'm aware of).&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; y(i)=diag(my_matrix(row(i),col(i)));&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; This is an extremely inefficient way to do it of course.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; John&lt;br&gt;
&lt;br&gt;
Actually, I've always found sub2ind to be quite slow.&lt;br&gt;
Consider:&lt;br&gt;
&lt;br&gt;
rows=randint(5,100000,[1,10]);&lt;br&gt;
cols=randint(5,100000,[1,10]);&lt;br&gt;
testMat=magic(10);&lt;br&gt;
&lt;br&gt;
%Sub2ind&lt;br&gt;
tic&lt;br&gt;
resultsInd=zeros(100000,5);&lt;br&gt;
for i=1:100000&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;index=sub2ind(size(testMat),rows(:,i),cols(:,i));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;resultsInd(i,:)=testMat(index);&lt;br&gt;
end&lt;br&gt;
toc&lt;br&gt;
&lt;br&gt;
%Diag&lt;br&gt;
tic&lt;br&gt;
resultsDiag=zeros(100000,5);&lt;br&gt;
for i=1:100000&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;resultsDiag(i,:)=diag(testMat(rows(:,i),cols(:,i)));&lt;br&gt;
end&lt;br&gt;
toc&lt;br&gt;
&lt;br&gt;
isequal(resultsInd,resultsDiag)&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
With the result:&lt;br&gt;
Elapsed time is 6.405682 seconds. (sub2ind)&lt;br&gt;
Elapsed time is 0.293780 seconds. (diag)&lt;br&gt;
&lt;br&gt;
ans =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1</description>
    </item>
    <item>
      <pubDate>Sat, 15 Nov 2008 03:24:02 -0500</pubDate>
      <title>Re: How to do vectorize this simple code?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239269#611019</link>
      <author>Husam Aldahiyat</author>
      <description>Your comparison is invalid.</description>
    </item>
    <item>
      <pubDate>Sat, 15 Nov 2008 03:58:01 -0500</pubDate>
      <title>Re: How to do vectorize this simple code?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239269#611024</link>
      <author>Kenneth Eaton</author>
      <description>&quot;Henry &quot; &amp;lt;henry@fullspectrumengineering.com&amp;gt; wrote in message &amp;lt;gfl212$m0t$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hmm all those suggestions don't seem to be very efficient compared to my original code of:&lt;br&gt;
&amp;gt; for i=1:num_elements&lt;br&gt;
&amp;gt;     y(i)=my_matrix(row(i),col(i));&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I just thought I could write it as a single line like y=my_matrix(row,col) where row and col are vectors and it would match them up as pairs and return a vector.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Seems like a common procedure and I'm surprised matlab doesn't have it built in...&lt;br&gt;
&lt;br&gt;
I don't know if SUB2IND is vectorized. If not, try this:&lt;br&gt;
&lt;br&gt;
N = size(my_matrix,1);&lt;br&gt;
ind = N*(col-1)+row;&lt;br&gt;
y = my_matrix(size(my_matrix,1)*(col-1)+row);&lt;br&gt;
&lt;br&gt;
hth,&lt;br&gt;
Ken</description>
    </item>
    <item>
      <pubDate>Sat, 15 Nov 2008 04:20:19 -0500</pubDate>
      <title>Re: How to do vectorize this simple code?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239269#611029</link>
      <author>Kenneth Eaton</author>
      <description>&quot;Kenneth Eaton&quot; &amp;lt;Kenneth.dot.Eaton@cchmc.dot.org&amp;gt; wrote in message &amp;lt;gflhc9$h1d$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I don't know if SUB2IND is vectorized. If not, try this:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; N = size(my_matrix,1);&lt;br&gt;
&amp;gt; ind = N*(col-1)+row;&lt;br&gt;
&amp;gt; y = my_matrix(size(my_matrix,1)*(col-1)+row);&lt;br&gt;
&lt;br&gt;
Oops, mistyped that a little; you can remove the first 2 lines, using the third as a one-liner, or make the third line: y = my_matrix(ind);&lt;br&gt;
&lt;br&gt;
Ken</description>
    </item>
    <item>
      <pubDate>Sat, 15 Nov 2008 10:39:02 -0500</pubDate>
      <title>Re: How to do vectorize this simple code?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239269#611055</link>
      <author>Per Sundqvist</author>
      <description>&quot;Henry &quot; &amp;lt;henry@fullspectrumengineering.com&amp;gt; wrote in message &amp;lt;gfkstu$1id$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I want to vectorize this simple snippet:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for i=1:num_elements&lt;br&gt;
&amp;gt;     y(i)=my_matrix(row(i),col(i));&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I tried y=my_matrix(row,col)&lt;br&gt;
&amp;gt; and y=my_matrix([row col])&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; but nothing seems to work.  Any pointers?  Thanks.&lt;br&gt;
&lt;br&gt;
This works, but its not so obvious and simple as it should be&lt;br&gt;
&lt;br&gt;
M=rand(4)&lt;br&gt;
row=(1:3);&lt;br&gt;
col=(2:4);&lt;br&gt;
y=diag(M(row,col))&lt;br&gt;
/Per</description>
    </item>
  </channel>
</rss>

