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:15:37 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 28
Message-ID: <hpp8ip$2cs$1@fred.mathworks.com>
References: <hpp13j$o1i$1@fred.mathworks.com> <hpp78k$ef6$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1270883737 2460 172.30.248.37 (10 Apr 2010 07:15:37 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 10 Apr 2010 07:15:37 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 2306903
Xref: news.mathworks.com comp.soft-sys.matlab:625291

"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

Interestingly, the Mathematica script creates an array <174504x4 double> and this script creates an array <176851x4 double>, I'm wondering if Mathematica was dropping some possible values, or vice a versa.

Thanks.