Code optimization (3 line function)

조회 수: 11 (최근 30일)
Ronald van den Berg
Ronald van den Berg 2015년 1월 28일
댓글: Titus Edelhofer 2015년 1월 28일
I need a function that efficiently applies a reflective bound to a signal: when the signal reaches the bound, it stays there until the signal increases again (example below).
I wrote a short function that applies such a bound to all signals in a matrix (code below).
My question is: does someone know a way to speed up this code? I'm applying it to matrices of size 5000x5000, which takes about a second per matrix. Since i have to do this on thousands of matrices, it would be nice to speed up the function. Thanks!
function Y = reflect(Y,b)
% apply reflecting bound to signals in matrix Y; columns are signals, rows time steps
mhist = zeros(1,size(Y,2)); % history of shifts applied in prev time steps
for ii=1:size(Y,1) % loop over time steps
m = max(b-Y(ii,:)-mhist,0); % compute required shift for current time step
Y(ii,:) = Y(ii,:) + mhist + m; % apply shift
mhist = mhist + m; % add shift to history
end
Here is an example of its output (red=original, black=after applying reflective bound at Y=-50):
The plot was produced using the following code:
rng(1);
X=0:.1:100;
Y=cumsum(normrnd(0,5,size(X)))-X;
plot(X,Y,'r');
hold on;
plot(X,reflect(Y',-50),'k-');
plot([X(1) X(end)],[-50 -50],'b');
  댓글 수: 2
luc
luc 2015년 1월 28일
Pre-allocating matrices with nan vallues.
m, Y and mhist change within the loop, you know their final size so before entering the loop try to create them and fill them with nan vallues.
function:nan(3,3)=[nan nan nan;nan nan nan;nan nan nan]
Ronald van den Berg
Ronald van den Berg 2015년 1월 28일
편집: Ronald van den Berg 2015년 1월 28일
Luc, thanks for your response, but i'm afraid this won't help. Preallocation helps when variables change size within a loop, but m, nor Y, nor mhist does so (m and mhist are vectors, Y is the input matrix which is changed row by row in the loop).

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

채택된 답변

Sean de Wolski
Sean de Wolski 2015년 1월 28일
편집: Sean de Wolski 2015년 1월 28일
If you have MATLAB Coder, this could be a potentially good candidate for C code generation and MEXing. It involves a loop with a persistent state that's updated on each iteration, something compiled languages are often faster with.
If you don't, post a zip file with the matrix and I'll benchmark it for you.
  댓글 수: 1
Ronald van den Berg
Ronald van den Berg 2015년 1월 28일
Thanks, this is exactly what we're trying now. And it seems to be pretty fast (about to do a comparison now) -- i don't think any clever .m function would be able to beat this good old C solution :)

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

추가 답변 (1개)

Titus Edelhofer
Titus Edelhofer 2015년 1월 28일
Hi,
one thing that comes to my mind: can you swap rows and columns, i.e., transpose your input matrix?
function Y = reflect(Y,b)
% apply reflecting bound to signals in matrix Y; columns are signals, rows time steps
mhist = zeros(size(Y,1),1); % history of shifts applied in prev time steps
for ii=1:size(Y,2) % loop over time steps
m = max(b-Y(:,ii)-mhist,0); % compute required shift for current time step
Y(:,ii) = Y(:,ii) + mhist + m; % apply shift
mhist = mhist + m; % add shift to history
end
That should be faster, since MATLAB likes columns better than rows ...
Titus
  댓글 수: 2
Ronald van den Berg
Ronald van den Berg 2015년 1월 28일
Oh, i didn't know that column operations are faster than row (this may have to do with the order in which matrix elements are stored in memory?). I'll try this. Even if it doesn't help here (since we have a mex solution now), this could be very useful to keep in mind for future functions. Thanks!
Titus Edelhofer
Titus Edelhofer 2015년 1월 28일
I'm curious to see the speed up you gain with the MEX/MATLAB Coder approach. And yes, matrices are stored columnwise in MATLAB (you see this e.g. with
A = magic(4);
A(:)
Therefore both from programming standpoint but also from memory access (cache!) working on columns is faster.
Titus

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by