Function to find all instances of a value in a matrix
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 )
Return -1 if no values were found, otherwise return "n" where n is the number of values found.
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"); } }
The data of NANUM can also be available in Find function.
origin.h