How to vectorize the loops
조회 수: 1 (최근 30일)
이전 댓글 표시
Dear all,
Could anyone tell me how to vectorize the follow loops:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/156540/image.png)
Best regards.
Yeping Sun
댓글 수: 4
Andrei Bobrov
2016년 9월 22일
편집: Andrei Bobrov
2016년 9월 22일
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.
답변 (1개)
Andrei Bobrov
2016년 9월 22일
편집: Andrei Bobrov
2016년 9월 22일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!