ocmath_convert_sparse_xyz_to_matrix

 

Description

This function fill a sparse matrix with Zs according to the scatters' x,y values.

Syntax

int ocmath_convert_sparse_xyz_to_matrix( UINT nSize, double * x, double * y, double * z, double * matResult, double dXmin, double dXStep, UINT nXlen, double dYmin, double dYStep, UINT nYlen, double dPrecision = 1.0e-8 )

Parameters

nSize
[input] the number of scatters to be considered;
x
[input] the x-position of the scatters
y
[input] the y-position of the scatters
z
[input] the value of the scatters
matResult
[output] the result matrix.
dXmin
[input] the minimum of the matrix cells in x-direction
dXStep
[input] the interval of the matrix cells in x-direction
nXlen
[input] the number of rows in the resultd matrix
dYmin
[input] the minimum of the matrix cells in y-direction
dYStep
[input] the interval of the amtrix cells in y-direction
nYlen
[input] the number of columns in the resulted matrix
dPrecision
[input] the precision used when determining if the points are on the grid;

Return

The number of scatters discarded by the conversion.

Examples

EX1

#include <wks2mat.h>
void ocmath_convert_sparse_xyz_to_matrix_ex1()
{
    double dPrecision = 1.0e-8; 
    UINT n = 10; 
    double a[]={1,1,1,2,2,4,3,3,3,2};
    double b[]={1,4,3,1,2,3,1,2,3,2};
    double c[]={5,3,7,3,6,5,0,4,3,7};
    double mat[100];

    double Xmin, Ymin, Xstep, Ystep, Xmax, Ymax;

    int nRet = ocmath_xyz_examine_data(n, a, b, c, dPrecision, dPrecision, &n, &Xmin,
                &Xstep, &Xmax, &Ymin, &Ystep, &Ymax, true);

    if(nRet == 1)
    {
        printf("X: %g\t%g\t%g\n ", Xmin, Xstep, Xmax);
        printf("Y: %g\t%g\t%g\n ", Ymin, Ystep, Ymax);

        int iX, iY;
        iX = Xstep > 0 ? (int)((Xmax - Xmin) / Xstep + 0.5) + 1 : 1; 
        iY = Ystep > 0 ? (int)((Ymax - Ymin) / Ystep + 0.5) + 1 : 1; 

        nRet = ocmath_convert_sparse_xyz_to_matrix(n, a, b, c,
                    mat, Xmin, Xstep, iX, Ymin, Ystep, iY, dPrecision);
        for(int i=0; i<iY; i++)
        {
            for(int j=0; j<iX; j++)
                printf("%g\t",mat[i*iX + j]);
            printf("\n");
        }
    }
}

EX2

void ocmath_convert_sparse_xyz_to_matrix_ex2(double dPrecision = 1.0e-8)
{
    // The active Worksheet.
    Worksheet wks = Project.ActiveLayer();
    wks.SetSize(-1, 3);    // To contain the results
    
    Dataset dsX(wks, 0), dsY(wks, 1), dsZ(wks, 2);
    
    vector vX(dsX), vY(dsY), vZ(dsZ);
    
    int nSize = vX.GetSize();    
    
    // Step 1: Remove duplicates
    nSize = ocmath_xyz_remove_duplicates(nSize, vX, vY, vZ, Remove_With_Mean, dPrecision);
    
    double dXmin,dXStep,dXmax,dYmin,dYStep,dYmax;

    // Step 2, 3: examine data and do the conversion if data are sparse.
    int iRet = ocmath_xyz_examine_data(nSize, vX, vY, vZ, dPrecision, dPrecision, NULL, &dXmin,&dXStep,&dXmax,&dYmin,&dYStep,&dYmax);

    if (iRet == 1)
    {
        int n;
        int iX, iY;
        vector<double> mat;
        
        iX = dXStep > 0 ? (int)((dXmax - dXmin) / dXStep + 0.5) + 1 : 1; 
        iY = dYStep > 0 ? (int)((dYmax - dYmin) / dYStep + 0.5) + 1 : 1; 

        mat.SetSize(iX*iY); // iX*iY NOT= nVar, for sparse
        
        // return the number of scatters discarded by the conversion. 
        n = ocmath_convert_sparse_xyz_to_matrix(nSize, vX, vY, vZ,
                    mat, dXmin, dXStep, iX, dYmin, dYStep, iY);
                    
        printf("%d scatters are discarded.\n", n);
        
        MatrixLayer ml;
        ml.Create();
        ml.SetNumRows(iY);
        ml.SetNumCols(iX);
        printf("%d * %d matrix is created.\n", iX, iY);

        Matrix M(ml);

        M.SetXMin(dXmin);
        M.SetXMax(dXmax);
        M.SetYMin(dYmin);
        M.SetYMax(dYmax);
        
        M.SetByVector(mat, true);
    }
}

Remark

See Also

ocmath_xyz_examine_data

header to Include

wks2mat.h

Reference