# 2.7.2.3 Converting Worksheet to Matrix

You may need to re-organize your data by converting from worksheet to matrix, or vice versa, for certain analysis or graphing needs. This page provides information and examples of converting worksheet to matrix, and please refer to Converting Matrix to Worksheet for the "vice versa" case.

## Worksheet to Matrix

Data contained in a worksheet can be converted to a matrix using a set of Gridding X-Functions.

The w2m X-Function converts matrix-like worksheet data directly into a matrix. Data in source worksheet can contain the X or Y coordinate values in the first column, first row, or a header row. However, because the coordinates in a matrix should be uniform spaced, you should have uniformly spaced X/Y values in the source worksheet.

If your X/Y coordinate values are not uniform spaced, you should use the Virtual Matrix feature instead of converting to a matrix.

The following example show how to perform direct worksheet to matrix conversion:

// Create a new workbook
newbook;
// Import sample data
string fname$= system.path.program$ +
"\samples\Matrix Conversion and Gridding\DirectXY.dat";
impasc;
// Covert worksheet to matrix, first row will be X and first column will be Y
w2m xy:=xcol xlabel:=row1 ycol:=1;
// Show X/Y values in the matrix window
page.cntrl = 2;

When your worksheet data is organized in XYZ column form, you should use Gridding to convert such data into a matrix. Many gridding methods are available, which will interpolate your source data and generate a uniformly spaced array of values with the X and Y dimensions specified by you.

The following example converts XYZ worksheet data by Renka-Cline gridding method, and then creates a 3D graph from the new matrix.

// Create a new workbook without sheets
newbook;
// Import sample data
string fname$= system.path.program$ +
"\samples\Matrix Conversion and Gridding\XYZ Random Gaussian.dat";
impasc;
// Convert worksheet data into a 20 x 20 matrix by Renka-Cline gridding method
xyz_renka 3 20 20;
// Plot a 3D color map graph
worksheet -p 242 cmap;