# 2.1.18.4 ocmath_convert_regular_xyz_to_matrix

## Description

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

## Syntax

int ocmath_convert_regular_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 )

## 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

## Return

OE_NOERROR for success or else other ERROR_CODE

## Examples

EX1

#include <wks2mat.h>
void ocmath_convert_regular_xyz_to_matrix_ex1()
{
double dPrecision = 1.0e-8;
UINT n = 9;
double a[]={1,1,1,2,2,2,3,3,3};
double b[]={1,2,3,1,2,3,1,2,3};
double c[]={5,3,7,3,6,5,0,4,3};
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 == 0)
{
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_regular_xyz_to_matrix(n, a, b, c,
mat, Xmin, Xstep, iX, Ymin, Ystep, iY);

for(int i=0; i<iY; i++)
{
for(int j=0; j<iX; j++)
printf("%g\t",mat[i*iX + j]);
printf("\n");
}
}
}

EX2

// YZ data points are loaded from the active Workshheet.
// A new MatrixLayer is created to show the output matrix.
void ocmath_convert_regular_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 regular.
int iRet = ocmath_xyz_examine_data(nSize, vX, vY, vZ, dPrecision, dPrecision, NULL, &dXmin,&dXStep,&dXmax,&dYmin,&dYStep,&dYmax);

if (iRet == 0)
{
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 = nVar, for regular

//     return true for success or else false
n = ocmath_convert_regular_xyz_to_matrix(nSize, vX, vY, vZ,
mat, dXmin, dXStep, iX, dYmin, dYStep, iY);

if (n == false) { printf("Error!\n"); return; }

// Create a MatrixLayer, and set its dimension and number of rows & cols.
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);
}
}