2.2.3.9.41 matrixbase::GetSubMatrix

Description

Get a subset of this matrix specified by 0 based column and row indices or indices vector. -1 means to last col/row

Syntax

BOOL GetSubMatrix( matrixbase & mbTarget, int c1 = 0, int c2 = -1, int r1 = 0, int r2 = -1 )

BOOL GetSubMatrix( matrixbase & mbTarget, const vector<uint> vnIndices, BOOL bRow = TRUE)

Parameters

mbTarget
[output] Matixbase derived object containing returned matrixbase subset
c1
[input] Begining column index, default is 0 (0 based offset)
c2
[input] Ending column index, (inclusive) default -1 is GetNumCols -1 (0 based offset)
r1
[input] Begining row index, default is 0 (0 based offset)
r2
[input] Ending row index, (inclusive) default -1 is GetNumRows -1 (0 based offset)
vnIndices
[input] Including the indices of the wanted rows/cols of sub matrix from source matrix.
bRow
[input] Default TRUE is to extract rows, else to extract columns.

Return

Returns TRUE on successful exit and FALSE on failure.

Examples

EX1

// Extract a submatrix
void matrixbase_GetSubMatrix_ex1()
{
    matrix<double> mat1 = { 
        {2,2,2,2,2},
        {2,1,1,1,2},
        {2,1,1,1,2},
        {2,2,2,2,2}                
    };
 
    MatrixPage MatPg1;
    MatPg1.Create("Origin");
    MatrixLayer MatLy1 = MatPg1.Layers(0);
    Matrix Mat1(MatLy1);
    printf("  Original matrix %s has been created.\n",Mat1.GetName());
    Mat1 = mat1;
 
    MatrixPage MatPg2;
    MatPg2.Create("Origin");
    MatrixLayer MatLy2 = MatPg2.Layers(0);
    Matrix Mat2(MatLy2);
 
    // Demonstration of GetSubMatrix to extract the central 2x3 area
    int rc=Mat1.GetSubMatrix(Mat2,1,3,1,2);
    if(!rc) 
        printf("  Error: GetSubMatrix on %s failed.\n",Mat1.GetName());
    else
        printf("  Extracted submatrix from (2,2) to (3,4) is %s.\n",Mat2.GetName());
    
    MatrixPage MatPg3;
    MatPg3.Create("Origin");
    MatrixLayer MatLy3 = MatPg3.Layers(0);
    Matrix Mat3(MatLy3);
 
    // Demonstration of GetSubMatrix to extract the central 2x4 area
    rc=Mat1.GetSubMatrix(Mat3,1,-1,1,2);
    if(!rc) 
        printf("  Error: GetSubMatrix on %s failed.\n",Mat1.GetName());
    else
        printf("  Extracted submatrix from (2,2) to (3,4) is %s.\n",Mat3.GetName());
}

EX2

// Extract submatrix from certain rows of a source matrix
void test_matrixbase_GetSubMatrix1()
{
	int ii,jj;
	int nRows = 5;
	int nCols = 5;
	
	vector<uint> vnIndices = {0, 2, 3, 4};
	
	matrix matSrc=
		{
			{1,2,3,4,5},
			{6,7,8,9,10},
			{11,12,13,14,15},
			{16,17,18,19,20},
			{21,22,23,24,25}
		};
		
	matrix matTarRow;
	
	// Demonstration of GetSubMatrix to extract certain rows from matrix
	bool bRowCheck = matSrc.GetSubMatrix(matTarRow, vnIndices, true);
	
	// Print out the submatrix.
	printf("matTarRow = \n");
	if(bRowCheck)
	{
		for(ii=0; ii<matTarRow.GetNumRows() && bRowCheck; ii++)
		{
			for(jj=0; jj<matTarRow.GetNumCols() && bRowCheck; jj++)
			{
				printf("%f ",matTarRow[ii][jj]);
			}
			printf("\n");
		}
	}
	
}

Remark

See Also

matrixbase::CopyTo

Header to Include

origin.h