Code covered by the BSD License  

Highlights from
NCHOOSE2 (v2.1 - jun 2008)

3.66667

3.7 | 3 ratings Rate this file 5 Downloads (last 30 days) File Size: 2.26 KB File ID: #20144

NCHOOSE2 (v2.1 - jun 2008)

by Jos (10584)

 

01 Jun 2008 (Updated 03 Jun 2008)

all combinations of two elements

| Watch this File

File Information
Description

NCHOOSE2 - all combinations of two elements
 
Y = NCHOOSE2(X) returns all combinations of two elements of the array X. It is the fast, vectorized version of NCHOOSEK(X,2). X can be any type of array.
 
Example:
nchoose2([10 20 30 40])
% -> 10 20
% 10 30
% 10 40
% 20 30
% 20 40
% 30 40

nchoose2({'a','b','c','d','e'})
% -> 'a' 'b'
% 'a' 'c'
% ...
% 'c' 'e'
% 'd' 'e'
 
See also nchoosek, perms
 combn, nchoose, allcomb (on the File Exchange)

(v2.0, may 2008)

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
nCtwo
This submission has inspired the following:
VChooseK

MATLAB release MATLAB 7 (R14)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (6)
02 Jun 2008 Urs (us) Schwarz

scholarly example of smart indexing, which should be studied by ML novices as well as cognoscenti alike...
us

02 Jun 2008 Urs (us) Schwarz

this did not copy/paste into the last review:

unfortunately, NCHOOSE2 errors with a single input, eg, nchoose2('a') - an unnecessary flaw, which quickly shall be corrected by jos, i'm sure...
us

03 Jun 2008 Jos (author)

Thanks us. this problem will be fixed in v2.1

06 Jun 2008 m p

eheh...

11 Jun 2008 David S

much faster than nchoosek! thanks

16 Dec 2009 Jan Simon

In the comments it is stated, that NCHOOSE2 is for "R13+". This means > R13.
To run this in R13, V must be a column vector:
  V = reshape(N-1:-1:2, [], 1);
  ri = cumsum([1; V]);
  ind(ri, 2) = [0; -V] + 1;
However, obviously nobody missed the R13 compatibility in the last 1.5 years. So I think there is no need to change the algorithm, but perhaps the comment to "R>=14".

Please login to add a comment or rating.
Updates
03 Jun 2008

fixed error with input having less than 2 elements

Tag Activity for this File
Tag Applied By Date/Time
matrices Jos (10584) 22 Oct 2008 10:04:23
matrices Jos (10584) 23 Oct 2008 06:15:46
nchoosek Jos (10584) 23 Oct 2008 06:15:46
combinations Jos (10584) 23 Oct 2008 06:15:46
pick Jos (10584) 23 Oct 2008 06:16:20
selection Jos (10584) 23 Oct 2008 06:16:20
two Jos (10584) 23 Oct 2008 06:16:20
get Jos (10584) 23 Oct 2008 06:16:20
permutation Jos (10584) 23 Oct 2008 06:16:20
perms Jos (10584) 23 Oct 2008 06:16:20
fast Jos (10584) 23 Oct 2008 06:16:20
unique Jos (10584) 23 Oct 2008 06:16:20

Contact us at files@mathworks.com