Daniel@0: function c = nchoose2(v, f) Daniel@0: %NCHOOSE2 All combinations of N elements taken two at a time. Daniel@0: % Daniel@0: % NCHOOSE2(1:N) or NCHOOSEK(V) where V is a vector of length N, Daniel@0: % produces a matrix with N*(N-1)/2 rows and K columns. Each row of Daniel@0: % the result has K of the elements in the vector V. Daniel@0: % Daniel@0: % NCHOOSE2(N,FLAG) is the same as NCHOOSE2(1:N) but faster. Daniel@0: % Daniel@0: % NCHOOSE2(V) is much faster than NCHOOSEK(V,2). Daniel@0: % Daniel@0: % See also NCHOOSEK, PERMS. Daniel@0: Daniel@0: % Author: Peter J. Acklam Daniel@0: % Time-stamp: 2000-03-03 13:03:59 Daniel@0: % E-mail: jacklam@math.uio.no Daniel@0: % URL: http://www.math.uio.no/~jacklam Daniel@0: Daniel@0: nargs = nargin; Daniel@0: if nargs < 1 Daniel@0: error('Not enough input arguments.'); Daniel@0: elseif nargs == 1 Daniel@0: v = v(:); Daniel@0: n = length(v); Daniel@0: elseif nargs == 2 Daniel@0: n = v; Daniel@0: else Daniel@0: error('Too many input arguments.'); Daniel@0: end Daniel@0: Daniel@0: [ c(:,2), c(:,1) ] = find( tril( ones(n), -1 ) ); Daniel@0: Daniel@0: if nargs == 1 Daniel@0: c = v(c); Daniel@0: end