-2

I have an array of strings, can be any length. How can I produce a boolean matrix (array of array of boolean values with the same width as the array of strings) that contains all combinations of booleans?

Result should look something like this

0 0 0 0
0 0 0 1
0 0 1 0
0 0 1 1
0 1 0 0
.
.
1 1 1 1

Edit: To elaborate a little, this is part of a solution to a larger problem to generate dynamic SQL to handle where clause conditions across multiple tables each with 2 versions. In my attempt to narrow down the issue I agree I might have been too terse, I apologize.

This is what I ended up doing with the help of JamJar00.

var sWhere = TranslateCriteriaToSQL(oUoW, oCriteria, false, false, DataBaseID, User);
var sResult = "";

var tables = asTables.ToArray();
int n = tables.Length;

List<bool[]> matrix = new List<bool[]>();
double count = Math.Pow(2, n);
for (int i = 0; i < count; i++)
{
     string str = Convert.ToString(i, 2).PadLeft(n, '0');
     bool[] boolArr = str.Select((x) => x == '1').ToArray();

     var sCondition = sWhere;
     for (var j = 0; j < boolArr.Length; j++)
     {
         if (boolArr[j])
         {
            sCondition = " OR (" + sCondition.Replace("[" + tables[j] + "]", "[" + tables[j] + "Pending" + "]") +
                         ")\n";
         }
     }
     sResult += sCondition;
}

}

aggaton
  • 3,066
  • 2
  • 25
  • 36
  • Are you sure you need the boolean array? Wouldn't bitmask be enough? – awesoon Apr 21 '16 at 16:16
  • Any representation would work. It would also be okay to loop through all permutations one at a time, i.e. a routine that just gives the next permutation and some kind of indication if it went okay, i.e. we are done. – aggaton Apr 21 '16 at 16:19
  • You have 2 ^n posible values. It could be a very big array. – Jose Luis Apr 21 '16 at 16:19
  • This could help: http://stackoverflow.com/questions/11326038/all-possible-combinations-of-a-2d-array?rq=1 – Jose Luis Apr 21 '16 at 16:21
  • There are many questions about all sorts of permutations/combinations. Please make sure to review them and clarify why none of existing approaches worked. (Showing at least some solution like `i++` to get next combination would make question better too) – Alexei Levenkov Apr 21 '16 at 16:53

4 Answers4

2

Your question isn't the most clear but I think this something like what you're after:

int n = 4;

List<bool[]> matrix = new List<bool[]>();
double count = Math.Pow(2, n);
for (int i = 0; i < count; i++)
{
    string str = Convert.ToString(i, 2).PadLeft(n, '0');
    bool[] boolArr = str.Select((x) => x == '1').ToArray();

    matrix.Add(boolArr);

    Console.WriteLine(String.Join(" ", boolArr.Select((x) => x ? "1" : "0")));
}

bool[][] arr = matrix.ToArray();

Where n is the width of the strings to create.

(Definitely not the most optimized code I've ever written...)

JamJar00
  • 339
  • 4
  • 13
  • Thanks for this, it seems to do what I was looking for, I have been staring myself blind for some reason, not seeing clearly the straight forward solution. – aggaton Apr 21 '16 at 17:03
1

Try this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication87
{
    class Program 
    {
        static void Main(string[] args)
        {
            string input = "abcdef";
            long max = (long)(Math.Pow(2, input.Length) - 1);

            for (long count = 0; count <= max; count++)
            {
                List<string> array = new List<string>();
                for (int j = input.Length - 1; j >= 0; j--)
                {
                    array.Add((count >> j & 1) == 0 ? "0" : "1");
                }
                Console.WriteLine(string.Join(" ", array.ToArray()));
            }
            Console.ReadLine();

        }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
1

With an input string s and bool array b:

 string s = "foo";
 bool[,] b = new bool[(int)Math.Pow(2, s.Length), s.Length];
 for (int i = 0; i < (int)Math.Pow(2, s.Length); i++)
 {
     for (int j = 0; j < s.Length; j++)
     {
         b[i, s.Length - 1 - j] = ((i & (int)Math.Pow(2, j)) > 0);
     }
 }
SlimsGhost
  • 2,849
  • 1
  • 10
  • 16
0

Appreciate this is an old question, however I found myself having to generate all permutations for an array of booleans and came up with the following:

bool[][] GeneratePermutations(int size)
{
    return Enumerable.Range(0, (int)Math.Pow(2, size))
        .Select(i =>
            Enumerable.Range(0, size)
                .Select(b => ((i & (1 << b)) > 0))
                .ToArray()
        ).ToArray();
}
Chris Pickford
  • 8,642
  • 5
  • 42
  • 73