How can I make this code run faster?

조회 수: 2 (최근 30일)
Conrado Neto
Conrado Neto 2020년 8월 25일
댓글: Conrado Neto 2020년 8월 26일
This code takes about 30s to run, and I will need to run it several times
Can anyone help me make it faster?
here is the code:
sum_Load = rand(1,1000); % For demonstration purposes
Pseudo_Load_Factor = rand(500,1000); % For demonstration purposes
Disp_Factor = rand(500,1000); % For demonstration purposes
M = 1000;
N = 500;
Disp_Inter = zeros(M,N);
for j = 1:M
Load_Inter = sum_Load(j);
for i = 1:N
idx = find(cummin(Pseudo_Load_Factor(i,:))<Load_Inter & cummax(Pseudo_Load_Factor(i,:))>Load_Inter,1,'first');
if isempty(idx)
Disp_Inter(j,i) = 0.10*7000;
else
Disp_Inter(j,i) = interp1(Pseudo_Load_Factor(i,(idx-1):idx),Disp_Factor(i,(idx-1):idx),Load_Inter);
end
end
end
Thanks a lot!!
  댓글 수: 3
Adam Danz
Adam Danz 2020년 8월 25일
You could also use the profiler to look into which sections are relatively slower.
Conrado Neto
Conrado Neto 2020년 8월 25일
In my codes sum_load, Pseudo_Load_Factor and Disp_Factor are obtained/imported from .dat files.
I've updated the question with a running code, using rand for those arrays

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

채택된 답변

Bruno Luong
Bruno Luong 2020년 8월 25일
편집: Bruno Luong 2020년 8월 25일
M = 1000;
N = 500;
P = 1000;
sum_Load = rand(1,M); % For demonstration purposes
Pseudo_Load_Factor = rand(N,P); % For demonstration purposes
Disp_Factor = rand(N,P); % For demonstration purposes
Disp_Inter = zeros(M,N);
for j = 1:M
Load_Inter = sum_Load(j);
A = Pseudo_Load_Factor-Load_Inter;
B = A(:,1:end-1).*A(:,2:end) < 0;
[Bm,idx] = max(B,[],2);
idxl = (1:N)'+(idx-1)*N;
idxr = idxl+N;
PLFR = Pseudo_Load_Factor(idxr);
w = (PLFR-Load_Inter)./(PLFR-Pseudo_Load_Factor(idxl));
DPI = w.*Disp_Factor(idxl) + (1-w).*Disp_Factor(idxr);
DPI(Bm==0) = 700;
Disp_Inter(j,:) = DPI;
end
  댓글 수: 5
Bruno Luong
Bruno Luong 2020년 8월 25일
Yes it's the firstpoint (the idx returned by MAX command is the first one). Otherwise mine result won't match yours.
Conrado Neto
Conrado Neto 2020년 8월 26일
perfect, thanks

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

추가 답변 (1개)

Steven Lord
Steven Lord 2020년 8월 25일
I have some off the top of my head suggestions that come from the structure of the code, but if you explain in words what Disp_Inter represents and/or the underlying problem you're trying to solve we may be able to suggest functions to help improve your algorithm and/or its implementation at a higher level.
%snip
for j = 1:M
Load_Inter = sum_Load(j);
for i = 1:N
idx = find(cummin(Pseudo_Load_Factor(i,:))<Load_Inter & cummax(Pseudo_Load_Factor(i,:))>Load_Inter,1,'first');
An obvious low-hanging fruit: since Pseudo_Load_Factor doesn't change inside the nested loops, compute the cumulative minimum and cumulative maximum of them once before entering the loop and index into those precomputed matrices inside the loop.
if isempty(idx)
Disp_Inter(j,i) = 0.10*7000;
Preallocate Disp_Inter to this constant value before the loop (though I'd use 700 rather than multiplying the integer value 7000 by the double precision approximation to one tenth.) That way you only need to change the value if idx is not empty.
%snip the rest of the code
  댓글 수: 1
Conrado Neto
Conrado Neto 2020년 8월 25일
편집: Conrado Neto 2020년 8월 25일
Sure
Disp_inter is a vector with values of displacements
each Disp_inter has a corresponding Pseudo_Load_Factor value
(It is a load x displacement curve)
Then, separately, I have another vector, sum_Load, with values of load that I need to find the corresponding displacement in this load displacement curve
since sum_Load may not correspond exactly to Pseudo_load_Factor, I interpolate to find the corresponding displacement.
look at the following figure:
I hope I was able to explain clearly, if not, let me know
thanks

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by