|
"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
|