Improving performance of interp1 function inside for loop

조회 수: 3 (최근 30일)
Arya
Arya 2016년 8월 23일
편집: Arya 2016년 8월 24일
Hello,
I'm looking for some insight on troubleshooting a matlab fcn bottleneck in a Simulink project. According to the code profiler this Matlab in-line function is (understandably) hogging the runtime:
function [preview,preview_a] = fcn(dp,X_matrix,rho_matrix)
%preallocate
rho_matrix_t = rho_matrix'
preview_a=zeros(1,185);
for i=1:dp
preview(i)=X+i;
preview_a(i)=interp1(X_matrix,rho_matrix_t,preview(i));
end
I understand the interp1 will inevitably consume some CPU time, but could there be some approach to improve the above code?
In the meantime I have these following in mind:
  1. Vectorization: building preview(1:dp) then do interp1 operation in batch (but I suspect interp1 doesn't support this)
  2. Using other built-in Simulink block that does equivalent jobs? (I've been looking at interp block, will see if it does the job).
Any suggestions are welcomed. :)
Regards,
Arya

채택된 답변

Walter Roberson
Walter Roberson 2016년 8월 23일
interp1() is fine with accepting a vector of points to interpolate at. Building preview ahead of time should work well.
  댓글 수: 1
Arya
Arya 2016년 8월 24일
편집: Arya 2016년 8월 24일
Indeed, it turns out interp1() is happy to accept vector points, this is what I did:
function [preview,preview_a] = fcn(dp,X_matrix,rho_matrix)
%preallocate
rho_matrix_t = rho_matrix'
preview_a=zeros(1,185);
i=1:dp
preview(i) = X+i
preview_a(i)=interp1(X_matrix,rho_matrix_t,preview(i));
Much simpler than I thought, and seem to accelerate the execution time a bit.
Cheers!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Simulink에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by