binomial.m

来自「一些常被用于教学或者参考的概率论的实例的源代码」· M 代码 · 共 31 行

M
31
字号
function     bc=binomial(top,bottom)
%Created by PJNahin for "Duelling Idiots"(6/3/98)
%BINOMIAL computes the binomial coefficient (top)
%                                           (bottom)
%which is equal to:  top!/((top-bottom)!(bottom)!).
%Both top and bottom must be non-negative integers
%with bottom <= top.
%
%
if top==0|top==1         %if top = 0 or 1 then
                         %top! = 1
   num=1;
else
   x=2:top;              %otherwise form 2x3x...xtop
   num=prod(x);          %to get top!
end
if bottom==0|bottom==1   %ditto for bottom!
   den1=1;
else
   x=2:bottom;
   den1=prod(x);
end
test=top-bottom;
if test==0|test==1        %ditto for (top-bottom)!
   den2=1;
else
   x=2:test;
   den2=prod(x);
end
bc=(num/den1)/den2;
   

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?