numl.Math.LinearAlgebra.Matrix.Sum C# (CSharp) Method

Sum() public static method

Computes the sum of either the rows or columns of a matrix and returns a vector.
public static Sum ( Matrix m, VectorType t ) : Vector
m Matrix Input Matrix.
t VectorType Row or Column sum.
return Vector
        public static Vector Sum(Matrix m, VectorType t)
        {
            if (t == VectorType.Row)
            {
                Vector result = new Vector(m.Cols);
                for (int i = 0; i < m.Cols; i++)
                    for (int j = 0; j < m.Rows; j++)
                        result[i] += m[j, i];
                return result;
            }
            else
            {
                Vector result = new Vector(m.Rows);
                for (int i = 0; i < m.Rows; i++)
                    for (int j = 0; j < m.Cols; j++)
                        result[i] += m[i, j];
                return result;
            }
        }

Same methods

Matrix::Sum ( Matrix m ) : double
Matrix::Sum ( Matrix m, int i, VectorType t ) : double

Usage Example

示例#1
0
        /// <summary>Estimates.</summary>
        /// <param name="X">The Matrix to process.</param>
        /// <param name="type">(Optional) the type.</param>
        public void Estimate(Matrix X, VectorType type = VectorType.Row)
        {
            int n = type == VectorType.Row ? X.Rows : X.Cols;
            int s = type == VectorType.Row ? X.Cols : X.Rows;
            Mu = X.Sum(type) / n;
            Sigma = Matrix.Zeros(s);
            
            for (int i = 0; i < n; i++)
            {
                var x = X[i, type] - Mu;
                Sigma += x.Outer(x);
            }

            Sigma *= (1d / (n - 1d));
        }
All Usage Examples Of numl.Math.LinearAlgebra.Matrix::Sum