Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Trace array of matrices
Date: Thu, 13 Aug 2009 21:00:20 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 25
Message-ID: <h61ut4$1uv$1@fred.mathworks.com>
References: <h61tsn$lbo$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1250197220 2015 172.30.248.35 (13 Aug 2009 21:00:20 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 13 Aug 2009 21:00:20 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:563233


"Rajgopal " <runraj@gmail.com> wrote in message <h61tsn$lbo$1@fred.mathworks.com>...
> Hi,
> 
> Is it possible to vectorize the sum of the trace of an array of 2-D matrices?
> 
> Currently I am doing it in a loop
> 
> A = ones(4,4,N)
> sum = 0;
> for i = 1:N
>  sum = sum + trace(A(:,:,i))
> end
> 
> Any suggestions?
> 
> Thanks a lot..
> Purpose - speed...since N is huge!

  You might try adding elements along the third dimension first, as in:

 s = trace(sum(A,3));

  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.

Roger Stafford