Using the C Math Library    

Creating Numeric Arrays

To create a numeric array, use any of the following mechanisms:

Using Numeric Array Creation Routines

The MATLAB C Math Library contains many routines that create various types of numeric arrays, including scalar arrays, vectors, matrices, multidimensional arrays and some commonly useful arrays.

Creating Scalar Arrays.   The simplest way to create an array is to use the mlfScalar() routine. When you pass this routine a numeric value, it creates an 1-by-1 numeric array containing the value, stored in double precision format. Whenever you have to pass a numeric value to a library routine, you can use mlfScalar().

Creating Two-Dimensional Arrays (Matrices).   Because two-dimensional arrays of double precision values are used so often in MATLAB, the library includes the routine mlfDoubleMatrix() that allows you to create a matrix of double precision values and initialize it with data. Note that you can use integers to specify the array dimensions; you do not need to convert these to arrays.

This code produces the following output.

In this code fragment, note that the values in the C array used to initialize the MATLAB array are not specified in numeric order because MATLAB stores arrays in column major order and C arrays are stored in row-major order. For more information about this, see Initializing a Numeric Array with Data.

You can also use the MATLAB API routine, mxCreateDoubleMatrix(), to create a matrix of doubles.

Creating Multidimensional Numeric Arrays.   To create a multidimensional numeric array, use the MATLAB API routine mxCreateNumericArray(). The arguments to this routine are:

For example, the following code fragment creates a three-dimensional array, with dimensions 3-by-3-by-2. The mxDOUBLE_CLASS argument specifies that the array should contain double precision values. For a complete list of these class specifiers, use the MATLAB Help Desk to view the mxClass_ID online reference page.

Note in the example that the arguments specifying the number of dimensions, ndim, and the size of these dimensions, dims, do not need to be converted into a MATLAB array. The MATLAB API routines accept integer arguments.

This code creates the following 3-by-3-by-2 array.

For information about initializing this array with data, see Initializing a Numeric Array with Data.

Creating Commonly Used Numeric Arrays.   The MATLAB C Math Library includes several routines that create commonly used multidimensional arrays:

With all these routines, the number of dimensions in the resulting array equals the number of non-NULL arguments passed to the routine. To illustrate, the following example passes three arguments to mlfOnes() to create a three dimensional array. Because these routines allow you to create arrays of any number of dimensions, you must signify the end of the argument list by specifying a NULL.

This code fragment creates the following array:

Creating Vectors of Number Sequences..   To create a one dimensional array (vector) that contains a number sequence, use the mlfColon() routine. This routine performs the same function as the MATLAB colon (:) operator.

For example, the following code fragment creates a vector of all the numbers between 1 and 10.

This code creates the following output.

You can optionally specify an increment between the values in the vector. For more information, see the mlfColon() reference page in the MATLAB C Math Library Reference online documentation.

Creating Numeric Arrays by Calling Arithmetic Routines

The MATLAB C Math Library arithmetic routines create numeric arrays as their output. For example, the sample application in Chapter 2 creates arrays by calling the library arithmetic routines, mlfFactor() and mlfIntersect().

Creating Numeric Arrays by Concatenation

You can create arrays by grouping together existing MATLAB arrays using concatenation. In MATLAB, you use the [] (brackets) operator to concatenate arrays vertically or horizontally. For example, you can use the following syntax in MATLAB to concatenate arrays. In this MATLAB example, the numeric values being concatenated are scalar arrays. The semicolon indicates that you want to create rows for vertical as well as horizontal concatenation.

The MATLAB C Math Library uses the mlfHorzcat() and mlfVertcat() routines to perform horizontal and vertical concatenation. You nest calls to mlfHorzcat() inside mlfVertcat() to create the same two-dimensional array in a C program.

Creating Multidimensional Numeric Arrays by Concatenation.   Using mlfVertcat() and mlfHorzcat(), you can only create two-dimensional arrays. To create a multidimensional numeric array through concatenation, you must use the mlfCat() routine. As arguments to mlfCat(), you specify the dimensions along which to concatenate the arrays.

For example, the following code fragment creates two matrices, and then concatenates them to create a three-dimensional array.

This program displays the following output.

Creating Numeric Arrays by Assignment

You can also create a numeric array by assigning a value to a location in the array, using the mlfIndexAssign() routine. The MATLAB C Math Library creates a numeric array large enough to accommodate the specified location or expands an existing array.

The following example is equivalent to the MATLAB statement,
A(2,2) = 17. The C character string "(?,?)" specifies the format of the index subscript. For more information about array indexing, see Chapter 5.

This call creates the array A and fills it with zeros before performing the assignment. The following output shows the array created by this code fragment.


 Numeric Arrays Initializing a Numeric Array with Data