Path: news.mathworks.com!not-for-mail From: <HIDDEN> Newsgroups: comp.soft-sys.matlab Subject: Re: Generating a huge array. Date: Sat, 10 Apr 2010 07:00:10 +0000 (UTC) Organization: The MathWorks, Inc. Lines: 26 Message-ID: <hpp7lq$jlh$1@fred.mathworks.com> References: <hpp13j$o1i$1@fred.mathworks.com> <hpp78k$ef6$1@fred.mathworks.com> Reply-To: <HIDDEN> NNTP-Posting-Host: webapp-05-blr.mathworks.com Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: fred.mathworks.com 1270882810 20145 172.30.248.35 (10 Apr 2010 07:00:10 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Sat, 10 Apr 2010 07:00:10 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 2306903 Xref: news.mathworks.com comp.soft-sys.matlab:625287 "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <hpp78k$ef6$1@fred.mathworks.com>... > "Uriah Stephenson-Ward" <uriah+spam@urijah.org> wrote in message <hpp13j$o1i$1@fred.mathworks.com>... > > ......... > > I'm trying to generate an array with 4 columns (w1,w2,w3,w4) which is populated with every possible combination from 0 to 1, for each column, such that the row sum's to 1. > > ......... > > Doing all that removal seems very wasteful of time and computer memory. How about something like this: > > n = 1000; % Temporarily obtain sums w1+w2+w3+w4 = n > M = zeros((n+1)*(n+2)*(n+3)/6,4); > ix = 0; > for w1 = 0:n > for w2 = 0:n-w1 > for w3 = 0:n-w1-w2 > ix = ix+1; > M(ix,:) = [w1,w2,w3,n-w1-w2-w3]; > end > end > end > M = M/n; % Now the rows must sum to 1. > > The value (n+1)*(n+2)*(n+3)/6 is the exact number of rows required. None have to be thrown away. I have done this using integers until the last step to avoid encountering round off errors with your decimal fractions. > > Roger Stafford Excellent. Thanks for this guys.