2.2.3.9.1 matrixbase::AllNonZeroAllNonZero
Description
Indicates whether or not an entire row or column of this matrix is comprised of all non-zero elements.
Syntax
int AllNonZero( vector<BOOL> & vAllNonZero, int nDim = 1, double dTol = DEFAULT_TOLERANCE )
Parameters
- vAllNonZero
- [output] The result vector
- nDim
- [input] The AllNonZero 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 is comprised of all non-zero elements
void matrixbase_AllNonZero_ex1()
{
vector<BOOL> vRowIsAllNonZeros, vColIsAllNonZeros;
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 is comprised of all non-zero elements
rc=mat1.AllNonZero(vRowIsAllNonZeros);
if(rc!=0)
printf(" AllNonZero failed. Error Code=%d\n",rc);
else{
for(int n = 0 ; n<vRowIsAllNonZeros.GetSize(); n++)
printf("%d ",vRowIsAllNonZeros[n]);
// The result is 1 0 0 0.
printf("\n");
}
// to indicate whether or not each row of this matrix is comprised of all non-zero elements
rc=mat1.AllNonZero(vColIsAllNonZeros,2);
if(rc!=0)
printf(" AllNonZero failed. Error Code=%d\n",rc);
else{
for(int n = 0 ; n<vColIsAllNonZeros.GetSize(); n++)
printf("%d ",vColIsAllNonZeros[n]);
// The result is 1 0 0.
printf("\n");
}
//if abs(element) < dTol then the element is considered to be zero
rc=mat1.AllNonZero(vColIsAllNonZeros, 2, 0.01);
if(rc!=0)
printf(" AllNonZero failed. Error Code=%d\n",rc);
else{
for(int n = 0 ; n<vColIsAllNonZeros.GetSize(); n++)
printf("%d ",vColIsAllNonZeros[n]);
// The result is 0 0 0.
printf("\n");
}
}
EX2
// Determin if all cell(s) in each matrix column (or row) are non-zeros.
void matrixbase_AllNonZero_ex2()
{
PageBase pgbase;
int rc, ii, jj;
vector<BOOL> vRowIsAllNonZeros;
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",Mat1.GetName());
Mat1=mat1;
rc=mat1.AllNonZero(vRowIsAllNonZeros);
if(rc!=0)
printf(" AllNonZero 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("AllNonZero");
Mat2.SetSize(1,vRowIsAllNonZeros.GetSize());
Mat2.SetRow(vRowIsAllNonZeros, 0); // Set 1st Row(as the index starts 0) of Mat2
printf("AllNonZero vector is placed in %s.\n",Mat2.GetName());
printf("Note that ALL cells in the column should be non-zero and non-NANUM,\n");
}
}
Remark
Indicates whether or not an entire row or column of this matrix is comprised of all non-zero elements. For matrices having an underlying base type of double or complex a specified tolerance is used to determine whether or not each element is zero.
See Also
matrixbase::AnyNonZero
Header to Include
origin.h
|