2.2.3.9.7 matrixbase::Concatenate

Description

Concatenate a matrix to this matrix placing the results in a third matrix.

Syntax

int Concatenate( int nDim, matrixbase & mSource, matrixbase & mResult )

Parameters

nDim
[input] Specifies whether the matrices are concatenated row wise (nDim=1) or column wise (nDim=2)
mSource
[input] Source matrix that is concatenated to this matrix
mResult
[output]The matrix resulting from the concatenation

Return

Returns 0 on success or one of the following error codes on failure.

-1=nDim is not 1 or 2

-2=The source matrices do not have the same number of columns (when concatenating row wise) or rows (when concatenating column wise)

Examples

EX1

void matrixbase_Concatenate_ex1()
{
    int rc;
    matrix mat1 = { 
        {1, 2, 3}, 
        {4, 5, 6}
    };
    matrix mat2 = {{7, 8, 9}};
    rc = mat1.Concatenate( 1, mat2, mat1 );
    if(rc!=0) printf("Error: Concatenate failed. Error Code=%d\n",rc);
	else{
		printf("The matrix is:\n");
		for(int ii=0; ii< mat1.GetNumRows(); ii++){
			for(int jj=0; jj< mat1.GetNumCols(); jj++) 
				printf("%g  ", mat1[ii][jj]);
			printf("\r\n");
		}
	}
}

EX2

#define NEWMAT(X) MatrixPage MatPg##X; MatPg##X.Create("Origin"); \
MatrixLayer MatLy##X = MatPg##X.Layers(0); Matrix Mat##X(MatLy##X); Mat##X = mat##X;

// Concatenate a matrix to another matrix
void matrixbase_Concatenate_ex2()
{
    int rc;
    matrix mat1 = { 
        {1, 2, 3}, 
        {4, 5, 6}, 
        {7, 8, 9}
    };
    matrix mat2 = { 
        {0, 0, 0}, 
        {0, 0, 0} 
    };
    matrix mat3 = { 
        {0, 0}, 
        {0, 0}, 
        {0, 0}
    }; 
    // Row-wise concatenation (mat1 and mat2) result:
    //    {1, 2, 3}
    //    {4, 5, 6}
    //    {7, 8, 9}
    //    {0, 0, 0}
    //    {0, 0, 0}
    //
    // Column-wise concatenation (mat1 and mat3) result:
    //    {1, 2, 3, 0, 0}
    //    {4, 5, 6, 0, 0}
    //    {7, 8, 9, 0, 0}
 
	NEWMAT(1);  // source matrix to be concatenated
	NEWMAT(2);	// source matrix to concatenate row-wise
	NEWMAT(3);	// source matrix to concatenate column-wise

	//output matrix for row-wise concatenation
    printf("Concatenation of %s to %s Row-wise.\n",Mat2.GetName(),Mat1.GetName());
    matrix mat4;  
    rc = mat1.Concatenate( 1, mat2, mat4 );  //concatenate mat2 to mat1 row wise (nDim=1)
    if(rc!=0) printf("Error: Concatenation(row-wise) failed.rc=%d\n",rc);
    else
    {
		NEWMAT(4);
        printf("    Result of concatenation row-wise is %s.\n",Mat4.GetName());
    }
 
    //output matrix for column-wise concatenation
    printf("Concatenation of %s to %s Column-wise.\n",Mat3.GetName(),Mat1.GetName());
    matrix mat5;  
    rc = mat1.Concatenate( 2, mat3, mat5 );  //concatenate mat3 to mat1 column wise (nDim=2), change nDim to 3 to see the result
    if(rc!=0) printf("Error: Concatenation(column-wise) failed.rc=%d\n",rc);
    else
    {
		NEWMAT(5);
        printf("    Result of concatenation column-wise is %s.\n",Mat5.GetName());
    }
    
    //The source matrices do not have the same number of columns or rows.
    printf("Concatenation of %s to %s Row-wise.\n",Mat3.GetName(),Mat1.GetName());
    matrix mat6;  
    rc = mat1.Concatenate( 1, mat3, mat6 );  //concatenate mat3 to mat1 row wise (nDim=1)
    if(rc!=0) printf("Error: Concatenation(column-wise) failed. \nThe source matrices do not have the same number of columns or rows. rc=%d\n",rc);
    else
    {
		NEWMAT(6);
        printf("    Result of concatenation column-wise is %s.\n",Mat6.GetName());
    }
}

Remark

Concatenate a matrix to this matrix placing the results in a third matrix. Matrices can be concatenated row wise or column wise. All three matrices must have the same underlying base type or a runtime error will be generated.

See Also

Header to Include

origin.h