Could you let me know how to vectorize the below for-loop ?
pigrid = zeros(knum,epnum,sepnum,znum,sznum);
for ki = 1:knum
for epi = 1:epnum
for sepi = 1:sepnum
for zi = 1:znum
for szi = 1:sznum
pigrid(ki,epi,sepi,zi,szi) = epgrid(epi,sepi)*zgrid(zi,szi)*kgrid(ki)^alpha-f;
end
end
end
end
end

댓글 수: 9

Adam Danz
Adam Danz 2018년 8월 6일
Please provide values of knum,epnum,sepnum,znum,sznum. If the current nested loop strategy works, why do you need to vectorize it? Assuming epgrid, zgrid, and kgrid and matricies, this should be really fast.
Stephen23
Stephen23 2018년 8월 6일
matwork's "Answer" moved here:
Thanks for your response.
knum=100,epnum=10,sepnum=5,znum=10,sznum=5
Yes. The code works and doesn't take much time. Just I want to see if vectorization helps save time.
Adam Danz
Adam Danz 2018년 8월 6일
편집: Adam Danz 2018년 8월 6일
Here's what I got so far. It's a simplification but still relies on 3 of your 5 loops.
for ki = 1:knum
for epi = 1:epnum
for sepi = 1:sepnum
pigrid(ki,epi,sepi,:,:) = epgrid(epi,sepi)*zgrid(1:znum,1:sznum)*kgrid(ki)^alpha-f;
end
end
end
if zgrid(1:znum,1:sznum) == zgrid, then a further simplification would be
for ki = 1:knum
for epi = 1:epnum
for sepi = 1:sepnum
pigrid(ki,epi,sepi,:,:) = epgrid(epi,sepi)*zgrid*kgrid(ki)^alpha-f;
end
end
end
Further vectorization would require a few repmat() and reshape() function calls to store the results in your current 5D variable and I'm not sure you're going to save time by doing that. Nevertheless, I'd love to see a complete vectorization of this from someone who has more time to fiddle with it.
icdi
icdi 2018년 8월 6일
Thanks! Your code works and is simpler than mine. This helps me.
Moved from Answers to here.
"Thanks for your response. znum is not 10 but 7. Sorry about my typo."
In that case, my other answer wouldn't have worked... hm... The only way I can see this loop go faster is to move the
kgrid(ki)^alpha
outside the nested loops to prevent recalculating this value many times.
OCDER
OCDER 2018년 8월 6일
By the way, just realized your ID name is "matwork" with a mathworks logo. Perhaps try changing your ID and logo so that it does not seem to impersonate MathWorks employees.
icdi
icdi 2018년 8월 6일
Oh I see. Thanks for your comment.
OCDER
OCDER 2018년 8월 6일
No problem. And make sure to accept @Adam's answer, once he copies it over to the Answer section :)
icdi
icdi 2018년 8월 6일
Yes yes. Thanks again for your advice!

댓글을 달려면 로그인하십시오.

 채택된 답변

Adam Danz
Adam Danz 2018년 8월 6일

0 개 추천

Creating a fully vectorized version of these nested loops seems either not possible or not practical. Here's a summary of some improvements listed in the comment section under the question.
This simplification still relies on 3 of your 5 loops.
for ki = 1:knum
for epi = 1:epnum
for sepi = 1:sepnum
pigrid(ki,epi,sepi,:,:) = epgrid(epi,sepi)*zgrid(1:znum,1:sznum)*kgrid(ki)^alpha-f;
end
end
end
if zgrid(1:znum,1:sznum) == zgrid, then a further simplification would be
for ki = 1:knum
for epi = 1:epnum
for sepi = 1:sepnum
pigrid(ki,epi,sepi,:,:) = epgrid(epi,sepi)*zgrid*kgrid(ki)^alpha-f;
end
end
end
@OCDER also recommended moving kgrid(ki)^alpha outside of the loop which would eliminate ~10000 exponential calculations.
Assuming kgrid(1:knum)==kgrid,
kgalpha = kgrid.^alpha; % or kgrid(1:knum).^alpha
for ki = 1:knum
for epi = 1:epnum
for sepi = 1:sepnum
pigrid(ki,epi,sepi,:,:) = epgrid(epi,sepi)*zgrid*kgalpha(ki)-f;
end
end
end
Lastly, you could remove the " -f" to after the loop like this:
kgalpha = kgrid.^alpha; %or kgrid(1:knum).^alpha
for ki = 1:knum
for epi = 1:epnum
for sepi = 1:sepnum
pigrid(ki,epi,sepi,:,:) = epgrid(epi,sepi)*zgrid*kgalpha(ki);
end
end
end
pigrid = pigrid - f;

댓글 수: 2

Adam Danz
Adam Danz 2018년 8월 6일
편집: Adam Danz 2018년 8월 6일
All of these versions have been tested with fake data where epgrid, zgrid, and kgrid were matrices with random numbers and the loop variables used the values you shared above. Additionally, you should test your current nested loop code with the simplified version you choose to ensure you've got the same values. To do that, you could use
isequal(pigrid, pigrid2).
icdi
icdi 2018년 8월 6일
Oh Thanks! I learned a lot from your comments. Also thanks for letting me know the way to compare the versions.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2018년 8월 6일

댓글:

2018년 8월 6일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by