MATLAB: Need help optimizing this code…

codeno loopsoptimizationremove loops

how can I do this without a loop:
(consider vectors w and s of length n)…
dTot = 0;
for i=1:n
for j=i+1:n
v = w(i) * w(j) * (s(i)) * (s(j));
dTot = dTot + v;
end
end
dTot = dTot * 2;

Best Answer

Variant
ws = w.*s;
dTot = bsxfun(@times,ws,triu(ones(numel(w),1)*ws.',1));
dTot = sum(dTot(:))*2;