1

I've run into a stall trying to put together some code to average out 10x10 subarrays of a 2D multidimensional array.

Given a multidimensional array

var myArray = new byte[100, 100];

How should I go about creating 100 subarrays of 100 bytes (10x10) each.

Here are some examples of the value indexes the subarrays from the multidimensional would contain.

         [x1,y1,x2,y2]
Subarray1[0,0][9,9]
Subarray2[10,10][19,19]
Subarray3[20,20][29,29]

Given these subarrays, I would then need to average the subarray values to create a byte[10,10] from the original byte[100,100].

I realize this is not unbelievably difficult, but after spending 4 days debugging very low-level code and now getting stuck on this would appreciate some fresh eyes.

2 Answers 2

1

Use this as a reference. I used ints just for ease of use. Code is untested. but the idea is there.

        var rowSize = 100;
        var colSize = 100;
        var arr = new int[rowSize, colSize];
        var r = new Random();
        for (int i = 0; i < rowSize; i++)
            for (int j = 0; j < colSize; j++)
                arr[i, j] = r.Next(20);
        for (var subcol = 0; subcol < colSize / 10; subcol++)
        {
            for (var subrow = 0; subrow < colSize/10; subrow++)
            {
                var startX = subcol*10;
                var startY = subrow*10;

                var avg = 0;

                for (var x=0; x<10; x++)
                    for (var y = 0; y < 10; y++)
                        avg += arr[startX + x, startY + y];

                avg /= 10*10;
                Console.WriteLine(avg);
            }
        }

It looks like you're new to SO. Next time try to post your attempt at the problem; it's better to fix your code.

Sign up to request clarification or add additional context in comments.

Comments

1

The only challenge is figuring out the function, that given the subarray index we're trying to populate, would give you the correct row and column indexes in your original 100x100 array; the rest would just be a matter of copying the values:

// psuedocode
// given a subarrayIndex of 0 to 99, these will calculate the correct indices
rowIndexIn100x100Array = (subarrayIndex / 10) * 10 + subArrayRowIndexToPopulate;
colIndexIn100x100Array = (subarrayIndex % 10) * 10 + subArrayColIndexToPopulate;

I'll leave it as an exercise to you to deduce why the above functions correctly calculate the indices.

With the above, we can easily map the values:

var subArrays = new List<byte[,]>();
for (int subarrayIndex = 0; subarrayIndex < 100; subarrayIndex++)
{
    var subarray = new byte[10, 10];
    for (int i = 0; i < 10; i++)
        for (int j = 0; j < 10; j++)
        {
            int rowIndexIn100x100Array = (subarrayIndex / 10) * 10 + i;
            int colIndexIn100x100Array = (subarrayIndex % 10) * 10 + j;
            subarray[i, j] = originalArray[rowIndexIn100x100Array, colIndexIn100x100Array];
        }
    subArrays.Add(subarray);
}

Once we have the 10x10 arrays, calculating the average would be trivial using LINQ:

var averages = new byte[10, 10];
for (int i = 0; i < 10; i++)
    for (int j = 0; j < 10; j++)
    {
        averages[i, j] = (byte)subArrays[(i * 10) + j].Cast<byte>().Average(b => b);
    }

Fiddle.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.