Tags: contains, efficieny, function, functions, hii, improve, loops, matlab, mex, program, programming, speedand, toconvert, via

Anonymous Functions

On Programmer » Matlab

5,395 words with 5 Comments; publish: Thu, 08 May 2008 01:46:00 GMT; (200124.02, « »)

Hi

I have a program that contains a lot of for loops and I wish to

convert the program to C via the mex function to improve the speed

and efficieny.

At the heart of the for loops is an evaluation of an anonymous

function that the user supplies. I have read from other postings that

I could use mexCallMatlab to evaluate the function, however, I have

also read that making calls using mexCallMatlab essentially mitigates

any benefits you may get from this approach.

Can anyone suggest a way of evaluating Matlab anonymous functions (or

something similar) directly in the c program without needing calls to

mexCallMatlab?

Any suggestions would be greatly appreciated.

Thanks,

Nick

All Comments

Leave a comment...

  • 5 Comments
    • Nick Denman wrote:

      >

      > Hi

      > I have a program that contains a lot of for loops and I wish to

      > convert the program to C via the mex function to improve the speed

      > and efficieny.

      > At the heart of the for loops is an evaluation of an anonymous

      > function that the user supplies. I have read from other postings

      > that

      > I could use mexCallMatlab to evaluate the function, however, I have

      > also read that making calls using mexCallMatlab essentially

      > mitigates

      > any benefits you may get from this approach.

      > Can anyone suggest a way of evaluating Matlab anonymous functions

      > (or

      > something similar) directly in the c program without needing calls

      > to

      > mexCallMatlab?

      > Any suggestions would be greatly appreciated.

      > Thanks,

      > Nick

      Whats wrong with the matlab function feval? (function evaluation).

      Why all the loops, try vectorize it and use matlab instead of C.

      /Per

      #1; Thu, 08 May 2008 01:47:00 GMT
    • Hi Per

      Thanks for the reply. However, it is an optimisation type routine and

      cannot be vectorized. It uses while loops too. Sorry for not

      mentioning this earlier.

      I have already written it in Matlab, using anonymous functions, and

      wish to see if it can be ported easily to C.

      > Whats wrong with the matlab function feval? (function evaluation).

      > Why all the loops, try vectorize it and use matlab instead of C.

      > /Per

      Cheers, Nick

      #2; Thu, 08 May 2008 01:48:00 GMT
    • "Nick Denman" <ngdenmanNOSPAM.matlab.todaysummary.com.gmail.REMOVETHIS.com> writes:

      > Hi

      > I have a program that contains a lot of for loops and I wish to

      > convert the program to C via the mex function to improve the speed

      > and efficieny.

      > At the heart of the for loops is an evaluation of an anonymous

      > function that the user supplies. I have read from other postings that

      > I could use mexCallMatlab to evaluate the function, however, I have

      > also read that making calls using mexCallMatlab essentially mitigates

      > any benefits you may get from this approach.

      > Can anyone suggest a way of evaluating Matlab anonymous functions (or

      > something similar) directly in the c program without needing calls to

      > mexCallMatlab?

      If you must maintain the anonymous function feature, or any

      user-specified function calls from MATLAB, you will not be able to

      effectively optimize this in C. The real win of a MEX file is the

      elimination of MATLAB function-call overhead, especially in an inner

      loop. In your case, you would still be incurring this overhead, so

      you probably would not see much gain.

      Peter Boettcher <boettcher.matlab.todaysummary.com.ll.mit.edu>

      MIT Lincoln Laboratory

      MATLAB FAQ: http://www.mit.edu/~pwb/cssm/

      #3; Thu, 08 May 2008 01:49:00 GMT
    • Hi Peter

      Thanks for the reply. I agree with what you say. What I wish to know

      is is there a (easy) way in C to emulate the behaviour of anonymous

      functions or user specified function calls.

      Cheers, Nick

      Peter Boettcher wrote:

      > If you must maintain the anonymous function feature, or any

      > user-specified function calls from MATLAB, you will not be able to

      > effectively optimize this in C. The real win of a MEX file is the

      > elimination of MATLAB function-call overhead, especially in an

      > inner

      > loop. In your case, you would still be incurring this overhead, so

      > you probably would not see much gain.

      >

      > --

      > Peter Boettcher <boettcher.matlab.todaysummary.com.ll.mit.edu>

      > MIT Lincoln Laboratory

      > MATLAB FAQ: <http://www.mit.edu/~pwb/cssm/>

      >

      #4; Thu, 08 May 2008 01:50:00 GMT
    • Nick Denman wrote:

      >

      > Hi Peter

      > Thanks for the reply. I agree with what you say. What I wish to

      > know

      > is is there a (easy) way in C to emulate the behaviour of anonymous

      > functions or user specified function calls.

      > Cheers, Nick

      > Peter Boettcher wrote:

      > to

      > the

      overhead,

      > so

      If you must run it in a "pimitive" enviorment, like "linux shell",

      you give the name of the function-file (a file which evaluates that

      function) in another "function-name"-file refering to it, which you

      read from the main file which do the optimization. Then you just

      cange in the "function-name"-file. Perhaps also you could write a

      shell-script if it "parameterized"?

      Per

      #5; Thu, 08 May 2008 01:51:00 GMT