Create a new multidimensional array and fill it with values ​​from other arrays

I will 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 create a new array, where each column will contain elements from each of the above arrays and the number of rows will be a combination of all the elements of the array above. in this case, the number of columns is 3 (because I have 3 arrays), and the number of columns is 6, since the combination of all the elements is 1 * 2 * 3 = 6. So, my new array will be:

int array_num = 3;
int[,] comb = new int[combinations, array_num];

I want to populate each column as follows: The first column will contain the elements of array_1 that will change each

int c0 = (combinations / array_1.Length);

elements. the second column will contain elements from array_2, which will change each

int c1=(c0/array_2.Length);

elements. And the third column will contain the elements from thw array_3 that will change each

int c2=(c1/array_3.Length);

. :

2     3     7    
2     3     5
2     3     6
2     4     7
2     4     5
2     4     6

, , , , .

+3
4
    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;
    }
+1

2D-, 2D- 3D- .

,

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] = ...
        }
    }
}
+1
    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;
    }
+1
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

        }

    }
}

, arraysize1 * arraysize2 * arraysize3 , .

. recursion.

0

All Articles