2.2.3.9.19 matrixbase::Find

Description

Function to find all instances of a value in a matrix

Syntax

int Find( vector<uint> & vecR, vector<uint> & vecC, double dLower, double dUpper = _ONAN, int nPrecision = 8, int r1 = 0, int c1 = 0, int r2 = -1, int c2 = -1 )

Parameters

vecR
[output] row indices of all cells in matData where value was found.
vecC
[output] column indices of all cells in matData where value was found.
dLower
[input] the lower bound for the value to be found. Set this to exact value when you just want to search for a single value and not use bounds.
dUpper
[input] the upper bound for the value to be found. Leave it as default (_ONAN) if searching for only a single value.
nPrecision
[input] sets the level of precision to be used in the search. This parameter will be ignored if dUpper isn't the default one.
r1
[input] row index of cell in matData from where to start the search - leave as 0 to start from the first cell
c1
[input] column index of cell in matData from where to start the search - leave as 0 to start from the first cell
r2
[input] row index of cell in matData up to which the search should be done - leave as -1 to search to last cell
c2
[input] column index of cell in matData up to which the search should be done - leave as -1 to search to last cell

Return

Return -1 if no values were found, otherwise return "n" where n is the number of values found.

Examples

EX1

// Find matrix elements within a range
void matrixbase_Find_ex1()
{
    int rc, ii,jj;

    vector<uint> vr;
    vector<uint> vc;
    matrix<double> mat1 = {
        {-3, -2, -1},
        {99,  0,  0},
        { 1,  2,  3}
    }
            
    for(ii=0; ii<3; ii++)
        for(jj=0; jj<3; jj++) 
            if(mat1[ii][jj]==99)
                mat1[ii][jj]=NANUM;  // set row=ii,col=jj to NANUM
        //  Input matrix is:
        //        {-3, -2, -1}
        //        {--,  0,  0}
        //        { 1,  2,  3}
        
        //   Find condition:  -1.0<= amd <=1.0
        //   Output matrix to present the results (Found=1, Otherwise=0) is:
        //        {0,  0,  1}
        //        {0,  1,  1}
        //        {1,  0,  0}

    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);
            
    rc=mat1.Find(vr, vc, -1, 1); // Demonstration of to Find to find elements of -1.0<=cell<=1.0
            
    if(rc==-1) 
        printf("  Error: Find failed.\n");
    else {
        printf("  Results of Find are presented in %s. (Found=1, Otherwise=0)\n",Mat2.GetName());
        printf("  Find condition: From:-1.0 To:1.0 (inclusive).\n");
        printf("  Find found %d elements.\n",rc);

        Mat2.SetSize(mat1.GetNumCols(),mat1.GetNumRows());
        for(ii=0; ii<mat1.GetNumCols(); ii++) 
            for(jj=0; jj<mat1.GetNumRows(); jj++) 
                Mat2[ii][jj]=0;
        printf("  Found Elements:\n");
        for(ii=0; ii<vr.GetSize(); ii++) {
            Mat2[vr[ii]][vc[ii]]=1;
            printf("    #%d) Cell(%d,%d)=%g\n",ii+1,vr[ii]+1,vc[ii]+1,mat1[vr[ii]][vc[ii]]); 
        }
        printf("  Note that the missing values are excluded from the result.\n");
    }
}

Remark

The data of NANUM can also be available in Find function.

See Also

Header to Include

origin.h