C library for mvnrnd and randn
4 views (last 30 days)
Show older comments
Hello,
does anybody know a library which provides more or less the same functions as mvnrnd and randn of Matlab? The library must be also usable in within a mexFunction.
Thank you!
2 Comments
Titus Edelhofer
on 3 Apr 2012
Hi Jane,
I'm getting curious your mexFunction questions: why do you need all the things MATLAB provides as mex functions? Or are the mex functions needed to compare MATLAB and your hand written C code? If the latter is the case, you might want to take a look at MATLAB Coder (www.mathworks.com/products/matlab-coder) instead of writing everything by hand ...
Titus
Accepted Answer
Jan
on 3 Apr 2012
For randn see e.g. Knuth v2, 3rd ed, p122.,
U1 and U2 are uniform on (0,1), then
X = Variance * cos(2*pi*U1) * sqrt(-2*ln(U2)) + Mean;
Or the same for SIN instead of COS.
You will find more resources by asking your favorite internet search engine.
Converting everything into C requires a lot of time for coding and testing. Buying the Coder might be cheaper in total. P-coding is even cheaper and it hides the source code efficiently. On the other hand each call to each toolbox function can be tracked by using the debugger. E.g. this cannot be hidden successfully:
if strcmp(input('Password', 's'), 'Password1')
granted=true;
end
x = rand(3, 4);
y = sin(x);
z = sum(y, 2);
This is just a meaningless example. It will need less than a minute to reveal this code when it is P-coded.
5 Comments
Titus Edelhofer
on 3 Apr 2012
One thing to notice about MATLAB Coder: it can also generate a MEX file that runs the generated C code. Doing this is a good way to check if the generated C code and the MATLAB code lead to the same results (up to numerical roundoff, of course).
And a fact that might give some confidence in the quality of the code: although MATLAB Coder came out with R2011a the first time, the technology behind has a longer history as "emlc" being a part of Real-Time Workshop (today Simulink Coder).
Jan
on 3 Apr 2012
@Jane: The tracking of toolbox functions is easy in P-files, but much more complicated in the Coder-created files.
More Answers (1)
Titus Edelhofer
on 3 Apr 2012
Hi Jane,
if protecting IP is your concern, you might want to try pcode: it comes with MATLAB and encrypcts the algorithm as well ...
Titus
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!