[Webinar] Streamline your web hosting managementRegister Today


Binary matrix for all possible unique results of X(i) variables

Posted on 2008-02-11
Medium Priority
Last Modified: 2013-11-25
How to generate a binary matrix for all possible permutations of 'i' variables X, where " i " can be any number between 1 and infinite. I know that the resultant matrix will have 2^ i unique answers, but don't know how to generate it.  Let me explain by example:

variables x1, x2 (i=2) each with a possible value of 1 or 0, so the resultant matrx would be:

X2  X1
0     0
0     1
1     0
1     1

but what if i=120 for example, how do we generate the matrix for x(i) from 1 to 120? There would be 2^ 120 results.

The logic is simple, but I can not seem to translate to code:

x4 x3 x2 x1
0    0   0   0
0    0   0   1
0    0   1   0
0    0  1    1
0   1   0   0
0   1   0    1
0    1  1   0
0   1   1   1
1   0   0   0
1    0   0   1
1    0   1   0
1    0  1    1
1   1   0   0
1   1   0    1
1    1  1   0
1   1   1   1

generate matrix 2^i by i size
start with all x(i) values at 0
get value of x(i)
  if x(i)=1
      increment i
  else set x(i)=1 then start checking values from beginning
if i reaches count of total variables for that row OR leftmost variable is 1 move to next row

or something like above
Question by:Brainstormer
  • 3
  • 2
LVL 11

Accepted Solution

cup earned 750 total points
ID: 20889758
I don't know the mathlab programming language but I can give it to you in vbscript.  If you stick it in a file, say bitpat.vbs and run it from the command line under windows using

cscript bitpat.vbs

You'll get your patterns
bits = 5 ' number of bits required
count = 5^2
for c = 0 to count
   disp = ""
   val = c
   for b = 1 to bits
      if (val and 1) = 1 then
         disp = " 1" & disp
         disp = " 0" & disp
      end if
      val = val \ 2 ' shift right
   WScript.echo disp

Open in new window


Author Comment

ID: 20921528
cup, thanks for the VB script. It works for i=5, but I tested it for 6, or 7 and it fell short and stopped before all final values were 1. It also uses a string logic that does not truly "read" the previous values, which is OK, but not the "smart" solution I am looking for.

PS: I refuse to believe no one has ever had to come up with this problem before, and a true solution must exist. Come one programming experts, take a swing!

Author Comment

ID: 20922598
I was able to resolve this using a MatLab function available here: http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=7147&objectType=FILE

I also attached it for review purposes. The function code was hard for me to understand, I would still be interested into a more logical approach to this that's more language independent and easier to understand.
LVL 11

Assisted Solution

cup earned 750 total points
ID: 20923165
Sorry - the 5^2 should have been

count = (2^ bits) - 1

As for your program,  the notation is beyond me.

Author Comment

ID: 20923611
that function is beyond me as well, but I only need to call it to get the desired results.

CUP, your updated script is working quite well, I will try to get a MatLab version of it up.

%combn.m function required
%define the matrix columns
%create the matrix M for all binary combinations
M=combn([0 1],i);
%display M on screen

Open in new window


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
Why WooCommerce is one of the majorly favored choices when it comes to having an eCommerce store. This article will acquaint you with some reasons that I believe make it one of the best eCommerce platforms available.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question