image thumbnail

nchoose2

version 3.2.0.1 (2.42 KB) by Jos (10584)
all combinations of two elements

2.7K Downloads

Updated 28 Feb 2019

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. When X is a scalar integer > 1, it returns X*(X-1)/2.

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)

Cite As

Jos (10584) (2021). nchoose2 (https://www.mathworks.com/matlabcentral/fileexchange/20144-nchoose2), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2018b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired by: nCtwo

Inspired: nchoose3, VChooseK

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!