matrixbase::AnyNonZero

Description

Indicates whether or not each row or column of this matrix contains any non-zero elements.

Syntax

int AnyNonZero( vector<BOOL> & vAnyNonZero, int nDim = 1, double dTol = DEFAULT_TOLERANCE )

Parameters

vAnyNonZero
[input] The result vector
nDim
[input] The AnyNonZero test can occur row wise (relative to the first dimension when nDim=1) or column wise (relative to the second dimension when nDim=2)
dTol
[input] The tolerance used to decide whether or not an element is zero (if abs(element) < dTol, then the element is considered to be zero)

Return

Returns 0 on success and a non-zero error code on failure.

Examples

EX1

//to indicate whether or not each row or column of this matrix contains any non-zero elements
void matrixbase_AnyNonZero_ex1()
{
    vector<BOOL> vRowAnyNonZeros, vColAnyNonZeros;
    matrix<double> mat1 = {
        {1, 1, 1, 0.001},
        {1, 1, 0, 0},
        {1, 0, 0, 0}
    };
 
    int rc; 
    // to indicate whether or not each  column of this matrix contains any non-zero elements
    rc=mat1.AnyNonZero(vRowAnyNonZeros); 
    if(rc!=0) 
        printf("AnyNonZero failed. Error Code=%d\n",rc);
    else{ 
        for(int n = 0 ; n<vRowAnyNonZeros.GetSize(); n++)
            printf("%d ",vRowAnyNonZeros[n]);
                        // The result is 1 1 1 1.
        printf("\n");
    }
 
    // to indicate whether or not each row of this matrix contains any non-zero elements
    rc=mat1.AnyNonZero(vColAnyNonZeros,2);
    if(rc!=0) 
        printf("AnyNonZero failed. Error Code=%d\n",rc);
    else{ 
        for(int n = 0 ; n<vColAnyNonZeros.GetSize(); n++)
            printf("%d ",vColAnyNonZeros[n]);
                        // The result is 1 1 1.
                printf("\n");
    }
    
    //if abs(element) < dTol then the element is considered to be zero
    rc=mat1.AnyNonZero(vRowAnyNonZeros, 1, 0.01);
    if(rc!=0) 
        printf("  AllNonZero failed. Error Code=%d\n",rc);
    else{ 
        for(int n = 0 ; n<vRowAnyNonZeros.GetSize(); n++)
                printf("%d ",vRowAnyNonZeros[n]);
                        // The result is 1 1 1 0.
    }
}

EX2

// Determin if any cell(s) in each matrix column (or row) are non-zeros.
void matrixbase_AnyNonZero_ex2()
{
    PageBase pgbase;
    int rc, ii, jj;
    vector<BOOL>  vRowHasAnyNonZeros;
 
    matrix<double> mat1 = {
        {1, 1, 0,  1,  1,  0, 99},
        {1, 1, 0,  1, 99, 99, 99},
        {1, 0, 0, 99, 99, 99, 99}
    };
    for(ii=0; ii<3; ii++)
        for(jj=0; jj<7; jj++) 
            if(mat1[ii][jj]==99)
                mat1[ii][jj]=NANUM;  // set row=ii,col=jj to NANUM
    // Input matrix:
    // {1, 1, 0,  1,  1,  0, --},
    // {1, 1, 0,  1, --, --, --},
    // {1, 0, 0, --, --, --, --}
 
    String MatWinName,MatLyName;
 
    MatrixPage MatPg1; 
    MatPg1.Create("Origin");
    MatWinName=MatPg1.GetName();
    MatrixLayer MatLy1 = MatPg1.Layers(0);
    MatLyName=MatLy1.GetName();
    Matrix Mat1(MatLy1);
    pgbase = Project.Pages(); // Get the active page
    pgbase.Rename("Original");
    printf("%s matrix is created.\n\n",Mat1.GetName());
    Mat1=mat1;

    rc=mat1.AnyNonZero(vRowHasAnyNonZeros);
    if(rc!=0) 
        printf("AnyNonZero failed. Error Code=%d\n",rc);
    else{            
        MatrixPage MatPg2;
        MatPg2.Create("Origin");
        MatrixLayer MatLy2 = MatPg2.Layers(0);
        Matrix Mat2(MatLy2);
        pgbase = Project.Pages(); // Get the active page
        pgbase.Rename("AnyNonZero");
 
        Mat2.SetSize(1,vRowHasAnyNonZeros.GetSize());
        Mat2.SetRow(vRowHasAnyNonZeros, 0);  // Set 1st Row(as the index starts 0) of Mat3
        printf("AnyNonZero vector is placed in %s.\n",Mat2.GetName());
        printf("Note that the NANUM cells are not taken into account as non-zero,\n");
    }
}

Remark

Indicates whether or not each row or column of this matrix contains any (one or more) non-zero elements. For matrices having an underlying base type of double or complex the machine based precision is used to determine whether or not any element is zero.

See Also

matrixbase::AllNonZero

header to Include

origin.h