File Exchange

image thumbnail

NCHOOSE2 (v2.1 - jun 2008)

version 1.0 (2.26 KB) by

all combinations of two elements

3.66667
3 Ratings

3 Downloads

Updated

View License

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)

Comments and Ratings (6)

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

David S

much faster than nchoosek! thanks

m p

eheh...

Jos (author)

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

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

Urs (us) Schwarz

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

MATLAB Release
MATLAB 7 (R14)
Acknowledgements

Inspired by: nCtwo

Inspired: VChooseK

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

» Watch video