Reputation: 57
i have let's say the following arrays..
int[] array_1 = new int[1] { 2 };
int[] array_2 = new int[2] { 3, 4 };
int[] array_3 = new int[3] { 7,5, 6 };
int combinations;
combinations = array_1.Length * array_2.Length * array_3.Length;
what i want to do is to create a new array where each column will contain elements from each of the above arrays and the number of rows will be the combination of all elements of the above array. in this case the number of columns is 3 (because i have 3 arrays) and the number of columns is 6, as combinations of all elements are 1*2*3=6.So, my new array is going to be:
int array_num = 3;
int[,] comb = new int[combinations, array_num];
I want to fill each column in the following way: The first column will contain elements from the array_1 array which will change every
int c0 = (combinations / array_1.Length);
elements. the second column will contain elements from the array_2 which will change every
int c1=(c0/array_2.Length);
elements. And the third column will contain elements from thw array_3 which will change every
int c2=(c1/array_3.Length);
elements. For this particular example the result comb array will be this:
2 3 7
2 3 5
2 3 6
2 4 7
2 4 5
2 4 6
I hope i made my problem clear and I'm looking forward to any suggestions about how should i start, since I'm new in programming.
Upvotes: 3
Views: 3122
Reputation: 35136
static int[,] Combine(params int[][] arrays)
{
int[][] combined = CombineRecursive(arrays).Select(x=>x.ToArray()).ToArray();
return JaggedToRectangular(combined);
}
static IEnumerable<IEnumerable<int>> CombineRecursive(IEnumerable<IEnumerable<int>> arrays)
{
if (arrays.Count() > 1)
return from a in arrays.First()
from b in CombineRecursive(arrays.Skip(1))
select Enumerable.Repeat(a, 1).Union(b);
else
return from a in arrays.First()
select Enumerable.Repeat(a, 1);
}
static int[,] JaggedToRectangular(int[][] combined)
{
int length = combined.Length;
int width = combined.Min(x=>x.Length);
int[,] result = new int[length, width];
for (int y = 0; y < length; y++)
for (int x = 0; x < width; x++)
result[y, x] = combined[y][x];
return result;
}
Upvotes: 1
Reputation: 35136
static int[,] Combine(params int[][] arrays)
{
int[] pointers = new int[arrays.Length];
int total = arrays.Aggregate(1, (m, array) => m * array.Length);
int[,] result = new int[total, arrays.Length];
for (int i = 0; i < arrays.Length; i++)
result[0, i] = arrays[i][0];
for (int y = 1; y < total; y++)
for (int x = arrays.Length - 1; x >= 0; x--)
{
++pointers[x];
for (int i = x; i >= 0; i-- )
if (pointers[i] >= arrays[i].Length)
{
pointers[i] = 0;
if (i>0)
pointers[i - 1]++;
}
result[y, x] = arrays[x][pointers[x]];
}
return result;
}
Upvotes: 1
Reputation: 18088
You are basically trying to reshape 3D positions into a 2D array, you need to calculate the 2D position from the 3D position and copy the values.
Use for loops, e.g.
for (int dim0 = 0; dim0 < array1.Length; dim0++)
{
for (int dim1 = 0; dim1 < array2.Length; dim1++)
{
for (int dim2 = 0; dim2 < array3.Length; dim2++)
{
var pos = (dim0 * array2.Length + dim2) * array3.Length + dim2;
var pos0 = pos / array2.Length / array2.Length;
var pos1 = pos % (array2.Length * array2.Length);
matrix[pos0, pos1] = ...
}
}
}
Upvotes: 1
Reputation: 24403
for i = 0 to arraysize1
{
for j = 0 to arraysize2
{
for k = 0 to arraysize3
{
array1[i] , array1[j] , array1[k] is what you are looking for
}
}
}
As you can see the the loop will run arraysize1 * arraysize2 * arraysize3 and generate all combinations for the case you just listed.
If the number of arrays are not known ahead of time. You will have to learn about recursion.
Upvotes: 0