How can I calculate the Chi-Squared CDF if I use MATLAB C-Coder?

2 次查看(过去 30 天)
My MATLAB function file calls "chi2cdf", and I need to convert the script to C. The MATLAB C-Coder tool does not support the use of chi2cdf (and also does not support the use of plain cdf, either). Do any of you know of a work-around?

回答(1 个)

Mike Hosea
Mike Hosea 2013-9-11
This function is now supported for code generation in R2013b.
  2 个评论
Aaron Chesir
Aaron Chesir 2013-9-11
It is assuring to know that The Math Works is continually adding capability to their products (in this case, MATLAB). But how do I work around the problem given that my version of MATLAB (2012?) does not support the function? After all, if my version of MATLAB had supported the chi2cdf function, I would not have posed the question.
Mike Hosea
Mike Hosea 2013-9-11
编辑:Mike Hosea 2013-9-11
I have no way of knowing whether you are under a maintenance agreement or not. Many users can upgrade when new releases come out. Since you apparently cannot, note that GAMMAINC is supported, and GAMMAINC will compute what is referred to as the "regularized gamma function" here
So, you could do it this way.
function p = mychi2cdf(x,a)
p = real(gammainc(max(x,0)/2,a/2));
I don't know if you cared about the possibility of negative x, but the MAX call takes care of that possibility. The wrapping with real() is only there because the output of GAMMAINC is always complex with MATLAB Coder, and we know that the imaginary part will be zero with non-negative real inputs.

请先登录,再进行评论。

产品

Community Treasure Hunt

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

Start Hunting!

Translated by