From octave-sources-request at bevo dot che dot wisc dot edu Fri Nov 28 15:47:33 2003 Subject: n_choose_r function From: Henry Gomersall To: octave-sources at bevo dot che dot wisc dot edu Date: Fri, 28 Nov 2003 08:29:47 -0600 --=-5sX9Uh2LCOOFQzpUdax9 Content-Type: text/plain Content-Transfer-Encoding: 7bit I have written a function that returns a matrix containing all the values of n choose r, with the different combinations in the rows. Its probably not the fastest way of doing it, but its short and does the job. its called by n_choose_r(n,r) Apologies if it already exists. Henry Gomersall -- Henry Gomersall Trinity College, Cambridge Mobile: 07764 756059 whg21 at cam dot ac dot uk http://www.heng.pwp.blueyonder.co.uk/ Please avoid sending me any WORD or POWERPOINT attachments. See http://www.fsf.org/philosophy/no-word-attachments.html "If your only contribution is to quote another, that is no contibution at all." --=-5sX9Uh2LCOOFQzpUdax9 Content-Disposition: attachment; filename=n_choose_r.m Content-Type: text/plain; name=n_choose_r.m; charset=iso-8859-1 Content-Transfer-Encoding: 7bit 1; function [comb_matrix, row] = calc_comb(comb_matrix, row, column, start, n, r) %while still room in columns above to hold values up to n... while(start <= n-(r-column)); comb_matrix(row, column) = start; if(column < r); [comb_matrix, row] = calc_comb(comb_matrix, row, column+1, start+1, n, r); endif; ++start; if(start <= n-(r-column)) ++row; idx = column-1; %copy previous row if working above first column while(idx>0); comb_matrix(row, idx) = comb_matrix(row-1, idx); --idx; endwhile; endif; endwhile; endfunction; function comb_matrix = n_choose_r(n, r); number_rows = factorial(n)/(factorial(r)*factorial(n-r)); number_columns = r; comb_matrix = zeros(number_rows, number_columns); comb_matrix = calc_comb(comb_matrix, 1, 1, 1, n, r); endfunction; --=-5sX9Uh2LCOOFQzpUdax9--