1.0

1.0 | 2 ratings Rate this file 117 downloads (last 30 days) File Size: 1.34 KB File ID: #24137

choose.m

by Phillip M. Feldman

 

17 May 2009 (Updated 19 May 2009)

Code covered by BSD License  

compute number of ways of choosing m objects from n distinct objects

Download Now | Watch this File

File Information
Description

choose(n,m) computes the number of ways of choosing m objects from n distinct objects. The simplest definition of choose(n,m) is n! / (m! * (n-m)!), but the following algorithm is somewhat less susceptible to overflow, and is faster than Matlab's builtin nchoosek function.

MATLAB release MATLAB 7.7 (R2008b)
Zip File Content  
Other Files choose.m,
license.txt
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (4)
17 May 2009 Darren Rowland

This is a poor submission to the FEX. It provides nothing that nchoosek does not, and computes the result in a way which is more susceptible to roundoff error. The input checks are inadequate and no warnings are given for large output values. Here is the file contents

function k= choose(n, m)
%
% choose(n,m) is the number of ways of choosing m objects from n
% distinct objects.

% Check input arguments:

if (nargin ~= 2)
   error('choose requires 2 arguments.');
end

if (m < 0 | m > n)
   error('m (second argument) must be between 0 and n, inclusive.');
end

% The simplest definition of choose(n,m) is n! / (m! * (n-m)!), but the
% following algorithm is somewhat less susceptible to overflow.

if (m >= n-m)
   k= prod(m+1:n) / prod(2:n-m);

else
   k= prod(n-m+1:n) / prod(2:m);
end

18 May 2009 John D'Errico

Why submit functions that do the same thing that existing utilities in MATLB do better?

nchoosek does the same computation, but does it better, since nchoosek offers more functionality, and already exists in every copy of matlab for as far back as I can recall.

This has little in the way of error checking. No H1 line.

Admittedly, if you are trying to compute large binomial coefficients, you will be far better off using a variety of other tools. But this one fails when it needs not do so.

>> nchoosek(1000,800)
Warning: Result may not be exact. Coefficient is greater than 1.000000e+15
 and is only accurate to 15 digits.
> In nchoosek at 66
ans =
     6.61715556065931e+215

>> choose(1000,800)
ans =
   NaN

19 May 2009 Phillip M. Feldman

Darren and John-

Most of what I do with Matlab is simulation. From the standpoint of a simulationist, nchoosek is probably more suitable in the development/debugging stage of a simulation, but once the development and debugging has been done, one wants the code to run as fast as possible. Below are the results of a simple timing comparison of nchoosek and choose:

>> mytime(0); for i=1:1e6; nchoosek(52,13); end; mytime;
19-May-2009 10:45:26 Timer initialized.
19-May-2009 10:46:01 Last step took 35.56 sec.; total elapsed= 35.56 sec.

>> mytime(0); for i=1:1e6; choose(52,13); end; mytime;
19-May-2009 10:46:11 Timer initialized.
19-May-2009 10:46:20 Last step took 8.70 sec.; total elapsed= 8.70 sec.

Note that my function runs over four times as fast as Matlab's builtin function. For the user who invokes nchoosek small numbers of times from the command line, the difference in speed is unimportant. But, when nchoosek or choose is being called millions or tens of milions of times from within a program, the difference becomes important. Please let me know if further explanation is required.

20 May 2009 Jos (10584)

If it is all about speed, remove the error checks! For larger numbers the following formula is more appropriate:
exp(gammaln(N+1) - gammaln(K+1) - gammaln(N-K+1)) ;

Please login to add a comment or rating.
Updates
19 May 2009

Fixed wording of header comment.

Tag Activity for this File
Tag Applied By Date/Time
choose Phillip M. Feldman 17 May 2009 19:15:46
combinatorics Phillip M. Feldman 17 May 2009 19:15:46
nchoosek Phillip M. Feldman 19 May 2009 15:06:53
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com