2.2.3.9.51 matrixbase::RemoveEmptyRows

Description

Remove empty rows from a matrix

Syntax

int RemoveEmptyRows(BOOL bOnlyRemoveEmptyRows = TRUE, vector<uint>& vnIndicesRemovedRows = NULL)

Parameters

bOnlyRemoveEmptyRows
[input] When TRUE only remove rows consisting entirely of missing values. When FALSE remove all rows containing one or more missing values
vnIndicesRemovedRows
[output] Return indices of the removed rows of the original matrix(0 offset)

Return

Returns -1 * the row number (1 based offset) of the first row that contains a missing value but that does not consist entirely of missing values (the first row that contains a missing value but that is not empty) otherwise the number of rows removed is returned.

Examples

EX1

// Remove empty rows from a matrix
void matrixbase_RemoveEmptyRows_ex1()
{
    int rc, ii,jj;
    
    matrix<double> mat1 = {
        { 1,  1,  1,  1},
        {99, 99, 99, 99},
        { 2,  2,  2,  2},
        {99, 99, 99, 99}
    };
    for(ii=0; ii<4; ii++)
        for(jj=0; jj<4; jj++)
            if(mat1[ii][jj]==99) mat1[ii][jj]=NANUM;
    // Original matrix:
    //    { 1,  1,  1,  1}
    //    {--, --, --, --}
    //    { 2,  2,  2,  2}
    //    {--, --, --, --}
    //
    // Result matrix:
    //    { 1,  1,  1,  1}
    //    { 2,  2,  2,  2}
    
    MatrixPage MatPg1;        
    MatPg1.Create("Origin");
    MatrixLayer MatLy1 = MatPg1.Layers(0);
    Matrix Mat1(MatLy1);
    Mat1 = mat1;
    printf("  The original matrix is %s.\n",Mat1.GetName());
    
    matrix mat2(mat1);    
    MatrixPage MatPg2;
    MatPg2.Create("Origin");
    MatrixLayer MatLy2 = MatPg2.Layers(0);
    Matrix Mat2(MatLy2);
    Mat2 = mat2;

    rc=Mat2.RemoveEmptyRows();  // Remove empty rows
    if(rc<0) 
        printf("  (Row# of the 1st non-empty row containing a NANUM in %s) = %d\n",
          Mat1.GetName(),rc*(-1));
    else
    printf("  The result of removing empty rows is in %s. Removed rows=%d.\n",
          Mat2.GetName(),rc);
}

EX2

// Remove all rows that contains a NANUM from a matrix
void matrixbase_RemoveEmptyRows_ex2()
{
    int rc,ii,jj;
    
    matrix<double> mat1 = {
        { 1,  1,  1,  1},
        {99, 99, 99, 99},
        {99,  2,  2,  2},
        {99, 99, 99, 99}
    };
    for(ii=0; ii<4; ii++)
        for(jj=0; jj<4; jj++)
            if(mat1[ii][jj]==99) mat1[ii][jj]=NANUM;
    // Original matrix:
    //    { 1,  1,  1,  1}
    //    {--, --, --, --}
    //    {--,  2,  2,  2}
    //    {--, --, --, --}
    //
    // Result matrix:
    //    { 1,  1,  1,  1}
    
    MatrixPage MatPg1;        
    MatPg1.Create("Origin");
    MatrixLayer MatLy1 = MatPg1.Layers(0);
    Matrix Mat1(MatLy1);
    Mat1 = mat1;
    printf("  The original matrix is %s.\n",Mat1.GetName());
    
    matrix mat2(mat1);    
    MatrixPage MatPg2;
    MatPg2.Create("Origin");
    MatrixLayer MatLy2 = MatPg2.Layers(0);
    Matrix Mat2(MatLy2);
    Mat2 = mat2;

    rc=Mat2.RemoveEmptyRows(FALSE);  // Remove all rows that contain a NANUM
    if(rc<0) 
        printf("  (Row# of the 1st non-empty row containing a NANUM in %s) = %d\n",
          Mat1.GetName(),rc*(-1));
    else
    printf("  The result of removing empty rows is in %s. Removed rows=%d.\n",
          Mat2.GetName(),rc);
}

Remark

Remove any row in the matrix that consists entirely of missing values (i.e. remove empty rows). Optionally remove any row that contains one or more missing values. This function is useful for removing missing values from data and for determining whether or not analysis can logically proceed on data that is paired row wise.

See Also

Header to Include

origin.h