I want to average an upcoming single array but having difficulty to write a good algorithm. And to clarify the question. I will receive single arrays as:
a1, b1, c1, ... ,a2, b2, c2 ...
And I want to perform averaging as follows(I know a to l is not 1024 but it is just for illustration):
In my case, as illustrated above, the matrix with is 1024 and height is 10. And the result I need is a single array with length 1024 where:
A = average of (a1 + a2 + a3 ... + a9)
B = average of (b1 + b2 + b3 ... + b9)
...
I'm using C# but any programming language would help I can translate it.
You can try this simple algorithm:
a. Create an array of length 1024 (let's say averages) to store the sum of all values for a column.
b. Loop over the input array and for each element add it to the sum of the respective column in the averages array. The index of the column can be calculated by (current index) % 1024.
c. Finally compute the average by dividing all elements by 10.
class HelloWorld {
public static void main(String[] args) {
// dummy input array
double[] array = new double[10240];
for(int i = 0; i < array.length; i++) {
array[i] = i;
}
// Array to store averages
double[] averages = new double[1024];
// Loop to calculate column sum.
for(int i = 0 ; i < array.length; i++) {
averages[i % 1024] += array[i];
}
// Loop to calculate the average.
for(int i = 0; i < averages.length; i++) {
averages[i] = averages[i] / 10;
System.out.print(averages[i] + ", ");
}
}
}