行の合計平均を調べたいのですが、行にゼロが表示された場合、行の平均が完了したときにその特定の列を残す必要があります。もっと明確にしましょう。私はマトリックスを言う
5 3 4 4 0
3 1 2 3 3
4 3 4 3 5
3 3 1 5 4
1 5 5 2 1
最初の行の行合計平均は、16/5 ではなく 16/4 にする必要があります。これは、「0」値が含まれているため、行 1 列 5 を残したからです。
私は次のコードを試しています。最初の行では問題なく動作しますが、残りの各行2〜5と各列5ではゼロではありませんが値を残します。
私のコードは次のとおりです。
int rows = 5;
int cols = 5;
float hostMatrix[] = createExampleMatrix(rows, cols);
System.out.println("Input matrix:");
System.out.println(createString2D(hostMatrix, rows, cols));
float sums[] = new float[rows];
for(int i=0;i<rows;i++){
float sum = 0,counter=0;
for(int j=0;j<cols;j++){
if(hostMatrix[j]==0){
sum += hostMatrix[i * cols + j];
}
else
{
sum += hostMatrix[i * cols + j];
counter++;
}
}
sum=sum/counter;
sums[i] = sum;
}
System.out.println("sums of the columns ");
for(int i=0;i<rows;i++){
System.out.println(" "+sums[i]);
}
私が受け取ったプログラムの出力は次のとおりです。
sums of the columns
4.0
3.0
4.75
4.0
3.5
私は次のように出力したい:
4.0
2.4
3.8
3.2
2.8
私が間違っているところを教えてください