<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.co.kr/matlabcentral/newsreader/view_thread/258472</link>
    <title>MATLAB Central Newsreader - Trace array of matrices</title>
    <description>Feed for thread: Trace array of matrices</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2013 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.co.kr/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Thu, 13 Aug 2009 20:43:03 +0000</pubDate>
      <title>Trace array of matrices</title>
      <link>http://www.mathworks.co.kr/matlabcentral/newsreader/view_thread/258472#673033</link>
      <author>Rajgopal </author>
      <description>Hi,&lt;br&gt;
&lt;br&gt;
Is it possible to vectorize the sum of the trace of an array of 2-D matrices?&lt;br&gt;
&lt;br&gt;
Currently I am doing it in a loop&lt;br&gt;
&lt;br&gt;
A = ones(4,4,N)&lt;br&gt;
sum = 0;&lt;br&gt;
for i = 1:N&lt;br&gt;
&amp;nbsp;sum = sum + trace(A(:,:,i))&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
Any suggestions?&lt;br&gt;
&lt;br&gt;
Thanks a lot..&lt;br&gt;
Purpose - speed...since N is huge!</description>
    </item>
    <item>
      <pubDate>Thu, 13 Aug 2009 21:00:20 +0000</pubDate>
      <title>Re: Trace array of matrices</title>
      <link>http://www.mathworks.co.kr/matlabcentral/newsreader/view_thread/258472#673039</link>
      <author>Roger Stafford</author>
      <description>"Rajgopal " &amp;lt;runraj@gmail.com&amp;gt; wrote in message &amp;lt;h61tsn$lbo$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Is it possible to vectorize the sum of the trace of an array of 2-D matrices?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Currently I am doing it in a loop&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; A = ones(4,4,N)&lt;br&gt;
&amp;gt; sum = 0;&lt;br&gt;
&amp;gt; for i = 1:N&lt;br&gt;
&amp;gt;  sum = sum + trace(A(:,:,i))&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Any suggestions?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks a lot..&lt;br&gt;
&amp;gt; Purpose - speed...since N is huge!&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;You might try adding elements along the third dimension first, as in:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;s = trace(sum(A,3));&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;By the way, never use the names of functions such as "sum" as the names of your variables.  That will get matlab (and you as well) confused.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Thu, 13 Aug 2009 21:12:02 +0000</pubDate>
      <title>Re: Trace array of matrices</title>
      <link>http://www.mathworks.co.kr/matlabcentral/newsreader/view_thread/258472#673043</link>
      <author>Bruno Luong</author>
      <description>One of many solutions:&lt;br&gt;
&lt;br&gt;
d=A(bsxfun(@plus,(0:size(A,3)-1)*16,(1:5:16).'));&lt;br&gt;
st = sum(d(:))&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Thu, 13 Aug 2009 23:15:20 +0000</pubDate>
      <title>Re: Trace array of matrices</title>
      <link>http://www.mathworks.co.kr/matlabcentral/newsreader/view_thread/258472#673075</link>
      <author>Rajgopal </author>
      <description>"Bruno Luong" &amp;lt;b.luong@fogale.findmycountry&amp;gt; wrote in message &amp;lt;h61vj2$hqj$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; One of many solutions:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; d=A(bsxfun(@plus,(0:size(A,3)-1)*16,(1:5:16).'));&lt;br&gt;
&amp;gt; st = sum(d(:))&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Bruno&lt;br&gt;
&lt;br&gt;
Thank you so much. That speeded up my code incredibly!</description>
    </item>
  </channel>
</rss>
