我想计算2D数组每列的总和。 到目前为止我得到了这个:
public static double[] colMean(double[][] array){
double arrayCopy[] = new double[array.length];
for(int i = 0; i < array.length; i++ ){
double colSum = 0;
for(int k = 0; k < array.length ; k++){
colSum = colSum + array[k][i];
}
arrayCopy[i] = colSum;
}
return arrayCopy;
}
答案 0 :(得分:0)
你在2d数组中循环如下:
for(int i = 0; i < array.length; i++ ){
double colSum = 0;
for(int k = 0; k < array[i].length ; k++){
colSum = colSum + array[i][k];
}
答案 1 :(得分:0)
//Create a destination array to store column sums
//I'm supposing every row has the same number of columns
for (int i=0; i<array[0].length; i++) {
destination_array[i]=0;
}
//Traversing the 2D array
for (int i=0;i<array.length; i++) {
//Adding each column value at the column accumulator
for (int k=0;k<array[i].length;k++ {
destination_array[k]+=array[i][k];
}
}
希望有所帮助