Info

此问题已关闭。 请重新打开它进行编辑或回答。

how to mex this recursion

1 次查看(过去 30 天)
DoVile Last Name:
DoVile Last Name: 2016-12-3
关闭: MATLAB Answer Bot 2021-8-20
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 个评论
John D'Errico
John D'Errico 2016-12-3
编辑:John D'Errico 2016-12-3
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.

回答(1 个)

Walter Roberson
Walter Roberson 2016-12-3
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.

此问题已关闭。

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by