nchoosek(n, k) function, permitted scalar values for n and k arguments
7 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
The R2015b (and older) Matlab documentation says that when n and k are scalars, they can be real and non-negative. However, for non-integer n I get the following result:
>> nchoosek(4.5, 2)
ans = Empty matrix: 0-by-2
>>
The correct answer is 4.5 * 3.5 / (1 * 2) = 7.8750. Am I doing something wrong?
Tomy
0 Commenti
Risposte (1)
John D'Errico
il 4 Ott 2015
Modificato: John D'Errico
il 4 Ott 2015
You can choose to define that operator in ANY way you like.
However, if you had bothered to read the help for nchoosek, you would have found this:
nchoosek Binomial coefficient or all combinations.
nchoosek(N,K) where N and K are non-negative integers returns N!/K!(N-K)!.
So to call your answer the correct answer is not at all correct. In fact, since nchoosek is defined as a classic binomial coefficient, your answer is not even "correct" in any sense, since factorials are not defined for non-integer input. But suppose we chose to use the gamma function to extend those factorials to allow non-integer input?
Classically, the binomial coefficient is defined as
nchoosek(N,K) = factorial(N)/(factorial(K)*factorial(N-K))
Lets rewrite this using the relation between factorial and the gamma function.
factorial(m) = gamma(m+1)
So now technically, your case would have
gamma(5.5)./gamma(2)/gamma(3)
ans =
26.171
Completely irrelevant as I said, since binomial coefficients are generally undefined for non-integer input. I can see what you did for the non-integer case. The reason why binomial coefficients are often written in the form they are is because some of the terms in the factorials cancel out in that fraction. That would not work in any case for non-integers.
I'll add that the documentation does indicate that N may be any general real, non-negative values. But that only applies when N is a vector. In the case where N is a vector, nchoosek actually generates all possible combinations of those N elements, taken K at a time.
nchoosek([1.1 2.2 3.3],2)
ans =
1.1 2.2
1.1 3.3
2.2 3.3
Admittedly, the documentation should be clearer here, to distinguish the case where N is a scalar.
I hope the documentation folks are listening in to this one.
2 Commenti
John D'Errico
il 5 Ott 2015
Modificato: John D'Errico
il 5 Ott 2015
You are wrong that nchoosek should be defined for non-integer inputs. It is what it is, and what the documentation says it is. That someone has chosen to define binomial coefficients for non-integer arguments is not completely relevant.
help nchoosek
That states very clearly that it expects integer inputs for N and K scalar.
Sadly, the doc for nchoosek is somewhat ambiguous. I've asked that this be forwarded to the documentation team to resolve the issue. Part of me wonders whether this might be a gradual move to allow non-integer scalar input, or if it was just sloppy documentation. My guess is the latter.
Vedere anche
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!