Hi
I have an algorithm which has several anonymous functions passed as parameters which are then combined depending on flags. Since the resulting function is called many times, this slows down my program quite significantly. Is there a way to "explicitely combine" two anonymous functions?
Mini Example:
% Single anonymous function
full1 = @(x)sin(x) + 3*x;% split anonymous function
g1 = @(x)sin(x);g2 = @(x)3*x;full2 = @(x)g1(x) + g2(x);%%Speed Test
N = 100000;disp('Explicit combination')ticfor i = 1:N full1(rand);endtocdisp('Multilevel combination')ticfor i = 1:N full2(rand);endtoc
returns
Explicit combinationElapsed time is 0.234414 seconds.Multilevel combinationElapsed time is 0.434422 seconds.
What I would like is to somehow have g1(x) + g2(x) be "combined explicitely" such that I end up with a version as "full1", and not "full2". Is there a way to do that?
Thanks!
Best Answer