Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

how to mex this recursion

1 vue (au cours des 30 derniers jours)
DoVile Last Name:
DoVile Last Name: le 3 Déc 2016
Clôturé : MATLAB Answer Bot le 20 Août 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 commentaire
John D'Errico
John D'Errico le 3 Déc 2016
Modifié(e) : John D'Errico le 3 Déc 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.

Réponses (1)

Walter Roberson
Walter Roberson le 3 Déc 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.

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by