Path: news.mathworks.com!not-for-mail
From: "Bruno Luong" <b.luong@fogale.findmycountry>
Newsgroups: comp.soft-sys.matlab
Subject: Re: vectorization advice
Date: Fri, 6 Apr 2012 06:37:16 +0000 (UTC)
Organization: FOGALE nanotech
Lines: 19
Message-ID: <jlm2us$l1j$1@newscl01ah.mathworks.com>
References: <c8lon7t5i1h992c2c1qninvmlrubea8h5o@4ax.com> <jlhlcn$bgf$1@newscl01ah.mathworks.com> <jc6sn7dlgfvqrre43nj444h3s189hlf3gk@4ax.com> <jllkck$340$1@newscl01ah.mathworks.com> <ksmsn7509sa72jcu2kf89qpi03n1hvj9ku@4ax.com>
Reply-To: "Bruno Luong" <b.luong@fogale.findmycountry>
NNTP-Posting-Host: www-02-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1333694236 21555 172.30.248.47 (6 Apr 2012 06:37:16 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 6 Apr 2012 06:37:16 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 390839
Xref: news.mathworks.com comp.soft-sys.matlab:763541


> n = 500;
> q=4;
> k = 3;
> e = randn(q,k,n); % not the real data, but a typical size
> H = randn(k,k); % not the real data either
> tempSum = zeros(q,q);
>         for i=1:n
>             tempSum = tempSum + e(:,:,i)*H*(e(:,:,i)');
>         end

Here is a vectorized code, using James's mtimesx 
http://www.mathworks.com/matlabcentral/fileexchange/25977

E = reshape(permute(e, [2 1 3]), k, []);
A = reshape(H*E, [k q n]);
Sum = sum(mtimesx(e, A),3)

% Bruno