From: Akashdeep Kamra on
Hi

I have developed a Monte Carlo simulation in Matlab. I have realized that one "for loop" is the slowest step in my routine. So I want to write a mex script in C for this "for loop". Can I use the standard Matlab library in my mex script? If the answer is yes, will it be computationally efficient?

Thanks and regards,
Akash
From: James Tursa on
"Akashdeep Kamra" <akashk(a)iitk.ac.in> wrote in message <hvolur$4qe$1(a)fred.mathworks.com>...
> Hi
>
> I have developed a Monte Carlo simulation in Matlab. I have realized that one "for loop" is the slowest step in my routine. So I want to write a mex script in C for this "for loop". Can I use the standard Matlab library in my mex script?

Yes.

> If the answer is yes, will it be computationally efficient?

The calls to the MATLAB or m-file functions from within a mex routine will not be any more efficient than calling them from MATLAB since the functions themselves are doing the same thing in either case. In fact, you might get a slower result in some cases since the result being passed back to a mex routine will not be allowed to be shared, whereas calling the same function from MATLAB may return a shared result.

Where you might get a savings is in the for loop overhead itself, since this can run quite a bit faster in a mex routine. Or if you are accessing array slices then the mex routine can run faster because it doesn't have to copy the data first whereas it does at the MATLAB level. It all depends on what, exactly, you are doing. To give a better response we would have to see your code.

James Tursa