0

我正在尝试计算 C# 中密集矩阵中每一列的算术平均值,但我想知道是否可以完成?

4

1 回答 1

0

元代码

mean(int[,] data, int j)    
   int sum = 0;    
   for i=0 to data.Length - 1
      sum += data [i, j]
   end     
   return sum / data.Length;

C# 代码

class Program
{
    static void Main(string[] args)
    {
        var data = new[,]
        {
            { 1.1, 2.2 }, 
            { 3.3, 4.4 }, 
            { 5.5, 6.6 }, 
            { 7.7, 8.8 }
        };

        double meanValue = Mean(data, 0);
    }

    public static double Mean(double[,] data, int columnIndex)
    {
        double sum = 0.0;
        int rowsCount = data.GetLength(0);

        for (int i = 0; i < rowsCount - 1; i++)
        {
            sum += data[i, columnIndex];
        }

        return sum / rowsCount;
    }
}
于 2012-06-20T17:54:59.107 回答