How to vectorize the loops
1 view (last 30 days)
Show older comments
Dear all,
Could anyone tell me how to vectorize the follow loops:
Best regards.
Yeping Sun
4 Comments
Andrei Bobrov
on 22 Sep 2016
Edited: Andrei Bobrov
on 22 Sep 2016
Your code should be:
ev = exp(dv(:))';
Hr = zeros(40,40);
for ii = 1:40
for jj = 1:40
Hr(ii,jj) = ev*delta_2(:,ii,jj);
end
end
Vectorize form see my answer in part 3.
Answers (1)
Andrei Bobrov
on 22 Sep 2016
Edited: Andrei Bobrov
on 22 Sep 2016
if dv -scalar:
out = permute(sum(delta*exp(dv)),[2,3,1]);
if dv - vector:
out = permute(sum(...
reshape(sum(bsxfun(@plus,reshape(delta,[],1),exp(dv(:)')),2),size(delta))...
),[2,3,1]);
part 3
ev = exp(dv)';
[~,n,k] = size(delta_2);
out = reshape(ev*reshape(delta_2,k,[]),n,[]);
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!