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)
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".
much faster than nchoosek! thanks
Thanks us. this problem will be fixed in v2.1
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...
scholarly example of smart indexing, which should be studied by ML novices as well as cognoscenti alike...