vectorizing or speeding looped code

17 次查看(过去 30 天)
Paul Schenk
Paul Schenk 2020-9-22
移动Rik 2026-2-4,13:58
i am trying to speed up some code with multiple functions and have found the one that takes the most time (it is all a converted fortran code). it runs a nested for loop 4 times (each with modified input). i've had some success elsewhere in the code eliminating slow points but this one just cant get right. any ideas? (in 2016b for compatability reasons)
for II= 1:N
XP(1)= 1.0D0;
for JJ=2:IORD1
XP(JJ)=XP(JJ-1)*double(A1(II));
end
for JJ= 1:IORD1
for KK= 1:IORD1
B(JJ,KK)=B(JJ,KK)+XP(JJ)*XP(KK);
end
C(JJ)=C(JJ)+XP(JJ)*A2(II);
end
end

回答(1 个)

Turlough Hughes
Turlough Hughes 2020-9-22
This should help, though I've already made assumptions about the sizes of arrays. How many rows/columns are in each variable?
XP(1) = 1;
for II= 1:N
XP(2:end) = XP(1:end-1)*double(A1(II));
B = B + XP(:).*XP(:).';
C = C + XP*A2(II);
end
  10 个评论
Turlough Hughes
Turlough Hughes 2020-9-23
移动:Rik 2026-2-4,13:58
eqor=1 ? I assume that's MAXOR?
Paul Schenk
Paul Schenk 2020-9-23
移动:Rik 2026-2-4,13:58
yes, my mistake!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Descriptive Statistics and Visualization 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by