matlab sum

May 31, 2012 at 1:49 PM

Hi, is there some equivalent of matlab sum ?

ex. given matrix m, sum(m,1) will give a vector where each element is sum of columns of matrix m.

Jun 15, 2012 at 8:44 AM

I don't think there is but it is straight forward to write one,

 

private double[] SumColumn(Matrix Data)
        {
            IEnumerable<Tuple<int,Vector<Double>>> onlyInFirstSet = Data.ColumnEnumerator();
            List<double> result = new List<double>();
            foreach (Tuple<int, Vector<Double>> temp in onlyInFirstSet)
            {
                result.Add(temp.Item2.Sum());
            }
            return result.ToArray();
        }

Sep 11, 2012 at 10:28 PM
Edited Sep 11, 2012 at 10:35 PM

There is another way, using mulitplication with a vector of same size in one of the dimension

double[,] dd = new double[,] { { 0, 3, 1, 10 }, { 1, 2, 4, 8 }, { 0, 0, -1, 2 } };

Matrix<double> D = new DenseMatrix(dd);

var v = new DenseVector(D.ColumnCount,1.0);  // Vector with entries 1.0
var w = new DenseVector(D.RowCount, 1.0);    // Vector with entries 1.0
Console.WriteLine("Column Sum {0}", D * v); Console.WriteLine("Row Sum {0}" ,w * D); Console.WriteLine("Sum all elements {0}",w * D * v); You always get out a vector out. (In MathNet you don't need to distunquish between Vectors that are (1 by m) or (m by 1) they are all Vectors size (m) )
Oct 11, 2012 at 12:04 AM
Edited Oct 21, 2012 at 9:42 AM

Actually before you use the code i wrote above, one persons column sum ( he reads it as the sum of columns from top to bottom per row) might not be the same as another persons row sum ( she reads it as the sum of all the rows for each column. ).

So have created the following extension methods for myself to avoid this confusion.

using MathNet.Numerics.LinearAlgebra.Generic;
using MathNet.Numerics.LinearAlgebra.Double;
using MathNet.Numerics;
using MathNet.Numerics.Statistics;

    public static class  MatrixExtensionMethods
    {

        public static Vector<double> MeanHorizontally(this Matrix<double> thisMatrix)
        {
            Vector<double> v = new DenseVector(thisMatrix.ColumnCount,1.0);
            Vector<double> sum = thisMatrix*v  ;
            return sum * (1.0 /(double) thisMatrix.ColumnCount);
        }

        public static Vector<double> MeanVertically(this Matrix<double> thisMatrix)
        {
            Vector<double> w = new DenseVector(thisMatrix.RowCount, 1.0);
            Vector<double> sum = w*thisMatrix;
            return sum * (1.0 / (double)thisMatrix.RowCount);
        }

        public static Vector<double> SumHorizontally(this Matrix<double> thisMatrix)
        {
            Vector<double> v = new DenseVector(thisMatrix.ColumnCount, 1.0);
            return  thisMatrix * v;
        }

        public static Vector<double> SumVertically(this Matrix<double> thisMatrix)
        {
            Vector<double> w = new DenseVector(thisMatrix.RowCount, 1.0);
            return w * thisMatrix;
        }

        public static Vector<double> StdHorizontally(this Matrix<double> thisMatrix)
        {
            var sDeviations = new DenseVector(thisMatrix.RowCount);
            foreach (int index in thisMatrix.RowIndices())
            {
                sDeviations[index] = thisMatrix.Row(index).StandardDeviation();
            }

            return sDeviations; 
        }

        public static Vector<double> StdVertically(this Matrix<double> thisMatrix)
        {
            var sDeviations = new DenseVector(thisMatrix.ColumnCount);
            foreach (int index in thisMatrix.ColumnIndices())
            {
                sDeviations[index] = thisMatrix.Column(index).StandardDeviation();
            }

            return sDeviations;
        }