m = 5000;
n = 1000;
p = 10000;
C = sprand(p,m,1e-2);
B = sprand(p,n,1e-2);
A = rand(p,1);
tic
(A.*B).'*C;
toc
p = size(B,1);
[ib,jb,b] = find(B.');
[ic,jc,c] = find(C.');
commonj = intersect(jb,jc);
keepb=ismember(jb,commonj);
keepc=ismember(jc,commonj);
ib = ib(keepb);
jb = jb(keepb);
b = b(keepb);
ic = ic(keepc);
jc = jc(keepc);
c = c(keepc);
[~,locb]=ismember(jb,commonj);
[~,locc]=ismember(jc,commonj);
cb = accumarray(jb,ib,[p 1],@(x) {x});
cc = accumarray(jc,ic,[p 1],@(x) {x});
ifun = @(cb,cc) reshape(cb(:)+(cc(:).'-1)*size(B,2),[],1);
ibc = cellfun(ifun, cb, cc, 'unif', 0);
jfun = @(j) j+zeros(size(ibc{j}));
jbc = arrayfun(jfun, (1:p).', 'unif', 0);
b = accumarray(jb,b,[p 1],@(x) {x});
c = accumarray(jc,c,[p 1],@(x) {x});
vfun = @(b,c) reshape(b(:).*(c(:).'),[],1);
vbc = cellfun(vfun, b, c, 'unif', 0);
ibc = cat(1,ibc{:});
jbc = cat(1,jbc{:});
vbc = cat(1,vbc{:});
BC = sparse(ibc,jbc,vbc,size(B,2)*size(C,2),p);
tic
reshape(BC*A,size(B,2),[]);
toc
Best Answer