v_choosenk

PURPOSE ^

V_CHOOSENK All choices of K elements taken from 1:N [X]=(N,K)

SYNOPSIS ^

function x=v_choosenk(n,k)

DESCRIPTION ^

V_CHOOSENK All choices of K elements taken from 1:N [X]=(N,K)
 The output X is a matrix of size (N!/(K!*(N-K)!),K) where each row
 contains a choice of K elements taken from 1:N without duplications.
 The rows of X are in lexically sorted order.

 To choose from the elements of an arbitrary vector V use
 V(CHOOSENK(LENGTH(V),K)).

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function x=v_choosenk(n,k)
0002 %V_CHOOSENK All choices of K elements taken from 1:N [X]=(N,K)
0003 % The output X is a matrix of size (N!/(K!*(N-K)!),K) where each row
0004 % contains a choice of K elements taken from 1:N without duplications.
0005 % The rows of X are in lexically sorted order.
0006 %
0007 % To choose from the elements of an arbitrary vector V use
0008 % V(CHOOSENK(LENGTH(V),K)).
0009 
0010 % CHOOSENK(N,K) is the same as the MATLAB5 function NCHOOSEK(1:N,K) but is
0011 % much faster for large N and most values of K.
0012 
0013 %   Copyright (c) 1998 Mike Brookes,  mike.brookes@ic.ac.uk
0014 %      Version: $Id: v_choosenk.m 10865 2018-09-21 17:22:45Z dmb $
0015 %
0016 %   VOICEBOX is a MATLAB toolbox for speech processing.
0017 %   Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html
0018 %
0019 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0020 %   This program is free software; you can redistribute it and/or modify
0021 %   it under the terms of the GNU General Public License as published by
0022 %   the Free Software Foundation; either version 2 of the License, or
0023 %   (at your option) any later version.
0024 %
0025 %   This program is distributed in the hope that it will be useful,
0026 %   but WITHOUT ANY WARRANTY; without even the implied warranty of
0027 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0028 %   GNU General Public License for more details.
0029 %
0030 %   You can obtain a copy of the GNU General Public License from
0031 %   http://www.gnu.org/copyleft/gpl.html or by writing to
0032 %   Free Software Foundation, Inc.,675 Mass Ave, Cambridge, MA 02139, USA.
0033 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0034 
0035 kk=min(k,n-k);
0036 if kk<2
0037    if kk<1
0038       if k==n
0039          x=1:n;
0040       else
0041          x=[];
0042       end
0043    else
0044       if k==1
0045          x=(1:n)';
0046       else
0047          x=1:n;
0048          x=reshape(x(ones(n-1,1),:),n,n-1);
0049       end
0050    end   
0051 else
0052    n1=n+1;
0053    m=prod(n1-kk:n)/prod(1:kk);
0054    x=zeros(m,k);
0055    f=n1-k;
0056    x(1:f,k)=(k:n)';
0057    for a=k-1:-1:1
0058       d=f;
0059       h=f;
0060       x(1:f,a)=a;
0061       for b=a+1:a+n-k
0062          d=d*(n1+a-b-k)/(n1-b);
0063          e=f+1;
0064          f=e+d-1;
0065          x(e:f,a)=b;
0066          x(e:f,a+1:k)=x(h-d+1:h,a+1:k);
0067       end
0068    end
0069 end

Generated by m2html © 2003