Info

This question is closed. Reopen it to edit or answer.

how to mex this recursion

1 view (last 30 days)
DoVile Last Name:
DoVile Last Name: on 3 Dec 2016
Closed: MATLAB Answer Bot on 20 Aug 2021
I am running a large program and a small function is taking up all the computing time, I havent been able to use the automatic c coder app because the function is recursive - what can I do ?
function f = fF(M1,M2,Integer)
%initialize to zero
TheSum = 0;
%loop with recursion
for n=1:(Integer-1)
TheSum = M1(Integer,n)*fF(M1,M2,n);
end
%puts output together
f = M2(:,Integer)-TheSum;
end
M1 and M2 are matrices, Integer is an Integer :)
  1 Comment
John D'Errico
John D'Errico on 3 Dec 2016
Edited: John D'Errico on 3 Dec 2016
Just creating a mex from this will not magically make it more efficient.
Instead, think about WHY it is spending so much time. We cannot really know anything, since you have told us nothing of value. What are these numbers? Just telling us they are integers is silly. 1 is an integer, so is zero. But your code will likely run quickly if Integer is 0 or 1. Tell us the actual values.
The crystal ball is so foggy.

Answers (1)

Walter Roberson
Walter Roberson on 3 Dec 2016
Your lines
for n=1:(Integer-1)
TheSum = M1(Integer,n)*fF(M1,M2,n);
end
can be written less recursively as
TheSum = M1(Integer, Integer-1) * fF(M1, M2, Integer-1) ;
with no loop. Are you sure that was your intention?
What is the purpose of your routine? It reminds me of calculating determinants.

Products

Community Treasure Hunt

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

Start Hunting!