View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
NCHOOSE2 (v2.1 - jun 2008)

Join the 15-year community celebration.

Play games and win prizes!

» Learn more

3.7 | 3 ratings Rate this file 1 Download (last 30 days) File Size: 2.26 KB File ID: #20144 Version: 1.0

NCHOOSE2 (v2.1 - jun 2008)


Jos (10584) (view profile)


01 Jun 2008 (Updated )

all combinations of two elements

| Watch this File

File Information

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.
nchoose2([10 20 30 40])
% -> 10 20
% 10 30
% 10 40
% 20 30
% 20 40
% 30 40

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

(v2.0, may 2008)


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

Jan Simon (view profile)

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".

Comment only
11 Jun 2008 David S

much faster than nchoosek! thanks

06 Jun 2008 m p


03 Jun 2008 Jos (author)

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

Comment only
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...

Comment only
02 Jun 2008 Urs (us) Schwarz

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

Contact us