2.2.3.9.68 matrixbase::SumColumns

Description

Sum the columns of this matrix placing the result in a vector.

Syntax

int SumColumns( vectorbase & vbSum )

Parameters

vbSum
[output] vector containing the summed columns

Return

Returns 0 on success or -1 on failure.

Examples

EX1

// Compute the column summation, and put the results into a vector
void matrixbase_SumColumns_ex1()
{
int rc, ii,jj;
matrix<double> mat1 = {
{-1,  0,  3,  99},
{ 0,  2,  6,   0},
{ 1,  4,  9,   0}
};
for(ii=0; ii<3; ii++)
for(jj=0; jj<4; jj++)
if(mat1[ii][jj]==99)
mat1[ii][jj]=NANUM;  // set row=ii,col=jj to NANUM
//  Input matrix is:
//        {-1, 0, 3, --}
//        { 0, 2, 6,  0}
//        { 1, 4, 9,  0}

//   Output column sum vector is:
//        { 0, 6, 18, --}

MatrixPage MatPg1;
MatPg1.Create("Origin");
MatrixLayer MatLy1 = MatPg1.Layers(0);
Matrix Mat1(MatLy1);
Mat1 = mat1;
printf("  Input matrix is %s.\n",Mat1.GetName());

MatrixPage MatPg2;
MatPg2.Create("Origin");
MatrixLayer MatLy2 = MatPg2.Layers(0);
Matrix Mat2(MatLy2);

vector vec1;

rc=Mat1.SumColumns(vec1); //Put the column summation to the vector
if(rc!=0)
printf("  Error: SumColumns failed. Error Code=%d\n",rc);
else {
Mat2.SetSize(1,vec1.GetSize());
Mat2.SetRow(vec1, 0);  // Place the vector to a single row matrix
printf("  Vector of column summation is placed in %s.\n",Mat2.GetName());
printf("  Note that a column including NANUMs results a missing in the vector.");
}
}

Remark

Sum the columns of this matrix placing the result in a vector. The underlying base type of the vector must not be less than the underlying base type of the matrix.

Note

The following is a summary of various matrix operations:

1) Multiplication:

1-1) * ... Proper multiplication of 2 matrices (mat1*mat2)

1-2) * ... Scaler multiplication of a matrix (mat1*A or A*mat1)

1-3) DotMultiply ... Element-wise multiplication of 2 matrices

1-4) Cross ... Cross product of 2 matrices

1-5) CumulativeProduct ... cumulative product of a matrix

2) Division:

2-1) / ... Not defined for 2 matrices. For proper division, multiply Inverse(mat1)

2-2) / ... Scaler division of a matrix (mat1/A)

2-3) DotDivide ... Element-wise division of 2 matrices

3-1) + ... Element-wise addition of 2 matrices

3-2) + ... Scaler addition of a matrix (mat1+A or A+mat1)

3-3) SumColumns ... Summation of each column in a matrix

3-4) CumulativeSum ... Cumulative product of a matrix

4) Subtraction:

4-1) - ... Element-wise subtraction of 2 matrices

4-2) - ... Scaler subtraction of a matrix (mat1-A or A-mat1)

4-3) Difference ... Difference of adjacent 2 rows in a matrix (1st order differential)

5) Power:

5-1) ^ (or pow) ... Not defined as element-wise power of 2 matrices

5-2) ^ (or pow) ... Not defined as scaler power of a matrix

5-3) DotPower ... Element-wise power of 2 matrices

To depict the differences of above additions, try the following sample:

1. define MAKEMAT(X) MatrixLayer MatLy##X;MatLy##X.Create();Matrix Mat##X(MatLy##X)

{

matrix mat1 = {{1, 2, 3},{1, 2, 3},{1, 2, 3}};

matrix mat2 = {{2, 3, 4},{3, 4, 5},{4, 5, 6}};

MAKEMAT(1); MAKEMAT(2); MAKEMAT(3); MAKEMAT(4); MAKEMAT(5); MAKEMAT(6);

Mat1=mat1; Mat2=mat2;

Mat3=mat1+mat2;

printf("%s = %s + %s <== Element-wise addition of 2 matrices\n",Mat3.GetName(),Mat1.GetName(),Mat2.GetName());

Mat4=mat1+10;

printf("%s = %s + 10 <== Scaler addition of a matrix\n",Mat4.GetName(),Mat1.GetName());

vector vec1;

Mat1.SumColumns(vec1);

Mat5.SetSize(1,vec1.GetSize());

Mat5.SetRow(vec1, 0);

printf("%s = SumColumns(%s) <== column summation of a matrix\n",Mat5.GetName(),Mat1.GetName());

mat1.CumulativeSum(Mat6);

printf("%s = CumulativeSum(%s) <== cumulative sum of a matrix\n",Mat6.GetName(),Mat1.GetName());

}