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