```Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: creating loops for a large system of variables
Date: Mon, 4 Apr 2011 00:39:04 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 35
Message-ID: <inb3v8\$4n6\$1@fred.mathworks.com>
References: <ina9ds\$qcn\$1@fred.mathworks.com> <inab9s\$o34\$1@fred.mathworks.com> <inaj1d\$j5b\$1@fred.mathworks.com> <inb174\$n3k\$1@fred.mathworks.com>
NNTP-Posting-Host: www-00-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1301877544 4838 172.30.248.45 (4 Apr 2011 00:39:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 4 Apr 2011 00:39:04 +0000 (UTC)
Xref: news.mathworks.com comp.soft-sys.matlab:719829

> Pretty elaborate. You couldhave  just use NDGRID, like below. But Roger is right. This will quickly become computationally unrealistic when you increase the number of variables.
>
> vars={[1,2],[3,4],[5,6]};
>
> [combinations{1:3}]=ndgrid(vars{1:3});
> combinations=cellfun(@(c) c(:), combinations,'uni',0);
> combinations=[combinations{:}]
>
> result=sum(combinations,2)
>
>
> combinations =
>
>      1     3     5
>      2     3     5
>      1     4     5
>      2     4     5
>      1     3     6
>      2     3     6
>      1     4     6
>      2     4     6
>
>
> result =
>
>      9
>     10
>     10
>     11
>     10
>     11
>     11
>     12

And that is why you guys are here.  Maybe one day I'll figure out this program.  But until then, I'll keep writing retarded code.
```