Code covered by the BSD License  

Highlights from
NCHOOSE2 (v2.1 - jun 2008)

3.66667

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

NCHOOSE2 (v2.1 - jun 2008)

by

 

01 Jun 2008 (Updated )

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

N Ctwo inspired this file.

This file inspired V Choose K.

MATLAB release MATLAB 7 (R14)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (6)
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".

11 Jun 2008 David S

much faster than nchoosek! thanks

06 Jun 2008 m p

eheh...

03 Jun 2008 Jos (author)

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

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

02 Jun 2008 Urs (us) Schwarz

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

Contact us