octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: nchoosek


From: Rik
Subject: Re: nchoosek
Date: Sun, 31 Aug 2014 18:35:18 -0700

On 08/31/2014 09:00 AM, address@hidden wrote:
> Le 31/08/2014 05:43, Rik a écrit :
>> nchoosek ([4 5], 8)
>
> With Matlab R2012a:
>
> -----------------------------------------------------------------
> >> nchoosek (5, 8)
>
> Error using nchoosek (line 48)
> K must be an integer between 0 and N.
>
> >> nchoosek ([4 5], 8)
>
> ans =
>
>    Empty matrix: 0-by-8
> -----------------------------------------------------------------
>
> I find this behaviour of Matlab rather unfortunate. Actually, I would
> have defined the output to be 0 and [0 0].
>
> Having nchoosek (n, k) be 0 when k > n is a very useful convention that
> make lots of combinatorial formulas easier to write (and prove). 

nchoosek is really two functions in one depending on whether the first
input is scalar.  If the first input is a scalar then the calculation is
the binomial coefficient so

nchoosek (5, 8)

would be the number of ways that 5 items can be put into groups of 8. 
Either we could treat this as a nonsensical question and return an error
message as Matlab does, or we could follow R and SciPy and say that there
are 0 ways to do this.  The default Octave behavior is to be Matlab
compatible, but we could change it if there are enough opinions that this
is really not the best behavior.

However, when the first input is a vector, the input is treated as a set. 
nchoosek returns all possible subsets of the original set which have the
desired property of being a unique combination of the original elements in
groups of k.

nchoosek ([4 5], 8)
is not
[ nchoosek(4, 8), nchoosek(5,8) ]

so I don't think it should return [0, 0].

Right now we return the empty set, which is true because that is the only
subset that matches.  I do find it odd that we give an error in the first
case and not in the second.  If I had to vote I would change the first case
to return 0 since that is close to the second case of returning the empty
set [].

--Rik



reply via email to

[Prev in Thread] Current Thread [Next in Thread]