Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Building a Logical Array For Factorial Application

Subject: Building a Logical Array For Factorial Application

From: Maxx Chatsko

Date: 5 Oct, 2012 16:26:07

Message: 1 of 6

Hello,

I have built a suite of programs that analyze massive data sets via distributed computing. I am trying to create a program to evaluate all of the possible combinations of an optical filter setup, which can be described as:

factorial(numChannels);

Ultimately, I would like to programmatically create a logical array [1,0] for all possible setups of my hardware - where 1 means the optical hardware is being used and 0 means the optical hardware is not being used. The logical array will determine the calculations needed to model the optical response of a system. Here is an example:

numChannels = 7;
stageArray = ones(factorial(numChannels), numChannels);

%Create all possible combinations with 6 channels on, 1 off
stageArray = [0,1,1,1,1,1,1;
                    1,0,1,1,1,1,1;
                    1,1,0,1,1,1,1;
                    1,1,1,0,1,1,1;
                    1,1,1,1,0,1,1;
                    1,1,1,1,1,0,1;
                    1,1,1,1,1,1,0];

I need to do the above programmatically and with all combinations of channels on and off (one off, two off, three off, four off, etc). It should also be flexible as numChannels will change between different setups.

I think I may need to break up each piece of the factorial. So all combinations of 6 channels on, 1 off could be built in a single for loop. Another loop could handle all combinations of 5 channels on, 2 off and so on. Any ideas?

Maxx

Subject: Building a Logical Array For Factorial Application

From: James Tursa

Date: 5 Oct, 2012 20:34:10

Message: 2 of 6

"Maxx Chatsko" wrote in message <k4n1mv$7du$1@newscl01ah.mathworks.com>...
> Hello,
>
> I have built a suite of programs that analyze massive data sets via distributed computing. I am trying to create a program to evaluate all of the possible combinations of an optical filter setup, which can be described as:
>
> factorial(numChannels);
>
> Ultimately, I would like to programmatically create a logical array [1,0] for all possible setups of my hardware - where 1 means the optical hardware is being used and 0 means the optical hardware is not being used. The logical array will determine the calculations needed to model the optical response of a system. Here is an example:
>
> numChannels = 7;
> stageArray = ones(factorial(numChannels), numChannels);
>
> %Create all possible combinations with 6 channels on, 1 off
> stageArray = [0,1,1,1,1,1,1;
> 1,0,1,1,1,1,1;
> 1,1,0,1,1,1,1;
> 1,1,1,0,1,1,1;
> 1,1,1,1,0,1,1;
> 1,1,1,1,1,0,1;
> 1,1,1,1,1,1,0];
>
> I need to do the above programmatically and with all combinations of channels on and off (one off, two off, three off, four off, etc). It should also be flexible as numChannels will change between different setups.
>
> I think I may need to break up each piece of the factorial. So all combinations of 6 channels on, 1 off could be built in a single for loop. Another loop could handle all combinations of 5 channels on, 2 off and so on. Any ideas?
>
> Maxx

If numChannels is not too large, you could use something like this to enumerate them:

dec2bin((0:(2^numChannels-1))')-'0' % caution, untested

If numChannels is too large, then the memory will blow up for this approach (but so will your runtime ... so this might not be a practical issue). I am not on a PC that has MATLAB at the moment so the above is untested.

James Tursa

Subject: Building a Logical Array For Factorial Application

From: Bruno Luong

Date: 5 Oct, 2012 21:32:09

Message: 3 of 6

Can't see where the factorial is from.

Bruno

Subject: Building a Logical Array For Factorial Application

From: Maxx Chatsko

Date: 5 Oct, 2012 22:55:06

Message: 4 of 6

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <k4njkp$iif$1@newscl01ah.mathworks.com>...
> Can't see where the factorial is from.
>
> Bruno

The factorial simply represents all the possible combinations of my hardware. If I have seven (7) channels that can be active or not active at one time, then the total amount of unique combinations is 7!. These include having one channel on six off, two channels on five off, three channels on four off.

Although now that I wrote that it seems it will be less than 7!. Perhaps only 3! or 4! since there will be repeats if I continue the thought of the last paragraph.

Thanks for bringing that to light

Subject: Building a Logical Array For Factorial Application

From: Bruno Luong

Date: 6 Oct, 2012 07:16:06

Message: 5 of 6

"Maxx Chatsko" wrote in message <k4noga$5b7$1@newscl01ah.mathworks.com>...

>
> The factorial simply represents all the possible combinations of my hardware. If I have seven (7) channels that can be active or not active at one time, then the total amount of unique combinations is 7!.

The number of all combinations of 7 channels on/off is 2^7, not 7!. James gave you the code to do that.

>These include having one channel on six off, two channels on five off, three channels on four off.

Don't understand this part.

>
> Although now that I wrote that it seems it will be less than 7!. Perhaps only 3! or 4! since there will be repeats if I continue the thought of the last paragraph.

You could provide perhaps a simple example with e.g., 3 channels. What is the logical array you want to get?

James's code give this

>> dec2bin((0:(2^3-1))')-'0'

ans =

     0 0 0
     0 0 1
     0 1 0
     0 1 1
     1 0 0
     1 0 1
     1 1 0
     1 1 1

Each row tells which channel is on (1) or off (0). There are 2^3=8 combination of those.

If you want to order the channels, then the number of combinations is 7!. This this is completely different problem, and one cannot store the ordering with a binary but with an array of permutations, i.e., (1:numberchannels), permuted in each row:

>> perms(1:3)

ans =

     3 2 1
     3 1 2
     2 3 1
     2 1 3
     1 2 3
     1 3 2

% Bruno

Subject: Building a Logical Array For Factorial Application

From: Maxx Chatsko

Date: 8 Oct, 2012 14:06:08

Message: 6 of 6

> If you want to order the channels, then the number of combinations is 7!. This this is completely different problem, and one cannot store the ordering with a binary but with an array of permutations, i.e., (1:numberchannels), permuted in each row:
>
> >> perms(1:3)
>
> ans =
>
> 3 2 1
> 3 1 2
> 2 3 1
> 2 1 3
> 1 2 3
> 1 3 2
>
> % Bruno

Nope, I need something along the lines of the dec2bin function. I'll work with that this morning and see what I can do. Thanks

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us