# 3.16.1 Get XY Coordinate of Min/Max Z From Matrix

## Version Info

Minimum Origin Version Required: Origin8 SR0

## Get XY Coordinate of Minimum/Maximum Z From Matrix

This example will show how to get the XY coordinate of the minimum/maximum Z value from a matrix.

void get_xy_coordinate_min_max_z()
{
// get the matrix object
MatrixLayer ml = Project.ActiveLayer();
if(!ml)
{
return;
}
MatrixObject mo = ml.MatrixObjects(0);
if(!mo)
{
out_str("Please make sure that there is a valid matrix object");
return;
}

// get number of column and row
int nCols = mo.GetNumCols();
int nRows = mo.GetNumRows();

// get matrix data and find the indices of min/max
Matrix& mat = mo.GetDataObject();
uint iStart = 0;  // start data index
uint iEnd = nCols*nRows-1;  // end data index
uint iMinIndex, iMaxIndex;

// get indices of min/max z
ocmath_d_minmax(mat, iStart, iEnd, &iMinIndex, &iMaxIndex);

// convert the indices to column/row indices
int iMaxRow = iMaxIndex/nCols;
int iMaxCol = iMaxIndex%nCols;
int iMinRow = iMinIndex/nCols;
int iMinCol = iMinIndex%nCols;

// get min and max z value
double dMin = mat[iMinRow][iMinCol];
double dMax = mat[iMaxRow][iMaxCol];

// get the xy coordinate for min/max z value
double dXMin = mat.GetXValue(iMinCol);
double dYMin = mat.GetYValue(iMinRow);
double dXMax = mat.GetXValue(iMaxCol);
double dYMax = mat.GetYValue(iMaxRow);

// print the result
printf("The minimum Z = %f\nThe maximum Z = %f\n", dMin, dMax);
printf("The XY coordinate of minimum Z is (%f, %f)\n", dXMin, dYMin);
printf("The XY coordinate of maximum Z is (%f, %f)\n", dXMax, dYMax);
}