Message Boards Message Boards

0
|
126 Views
|
2 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Replacement for Nested for statments

I am trying to explicitly write out the sample space for a variable number of components. The function would take a list as an input that provided the number of settings available to each component. A few examples to clarify what I am trying to do:

A={2,1,1}  (*three components, the first has potential settings of 0,1,2, the second and third each have possible settings of 0 and 1*)

B={3,2} (*two components; the first with 0,1,2,3 the second with 0,1,2 as settings *)

C={1,1,1,1} (*four components; all have settings of 0 and1*)

Output using A:

{{0,0,0},{0,1,0},{0,0,1},{0,1,1},
{1,0,0},{1,1,0},{1,0,1},{1,1,1},
{2,0,0},{2,1,0},{2,0,1},{2,1,1}}

Output using B:

{{0,0},{0,1},{0,2},
{1,0},{1,1},{1,2},
{2,0},{2,1},{2,2},
{3,0},{3,1},{3,2}}

Output using C:

{{0,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1},{0,1,1,0},{0,1,0,1},{0,0,1,1},{0,1,1,1},
{1,0,0,0},{1,1,0,0},{1,0,1,0},{1,0,0,1},{1,1,1,0},{1,1,0,1},{1,0,1,1},{1,1,1,1}}

If I knew the number of components in advance, I could write a set of nested for loops that will do this. Unfortunately, I do not know the number of components in advance.  So, I need something a little more flexible. Unfortunately, it seems to exceed my abilities. Any assistance would be helpful.

If it makes any difference when there are different numbers of settings for components they will always be listed in order of decreasing number of settings, i.e. {3,2,1} never {1,2,3}.

POSTED BY: Edward Davis
2 Replies

Thank you very much!!!!

I had no idea it was so simple.

This is exactly what I need.

POSTED BY: Edward Davis
Posted 2 days ago

You could use Tuples:

POSTED BY: Hans Milton
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract