Using the C++ Math Library    

Creating Numeric Arrays

You can create a numeric array in a C++ program by:

The following sections provide more detail about using each of these mechanisms, highlighting areas where the C++ syntax is significantly different from the corresponding MATLAB syntax.

Creating Arrays with C++ Constructors 

As a C++ class, the mwArray interface includes many useful constructors that allow you to create many different types of array. There is no MATLAB equivalent for a constructor.

When you declare a mwArray object, as in the following

you invoke the default constructor which creates a uninitialized array.

The mwArray object supports other constructors that accept various combination of arguments that allow you to create numerical scalar arrays or copy an existing array. (For a complete list of all mwArray constructors, see Constructors in Chapter 10.)

The following example uses the mwArray matrix constructor to create an 2-by-3 matrix, initialized to the values in a C++ array of double precision values. You can also use C++ arrays of integers or unsigned short values to initialize a MATLAB mwArray. This constructor can optionally take a second C++ array to initialize the imaginary part of an array of complex numbers. The example then uses the mwArray copy constructor to make a copy of the 2-by-3 array.

This code produces the following output:

Creating Multidimensional Arrays with Constructors.   

You cannot create an array of more than two dimensions using an mwArray constructor. Use an array creation routine or concatenation to create multidimensional arrays. Alternately, you can create a two-dimensional array using a constructor and then change it into a multidimensional array using the reshape() routine. For more information about the reshape() routine, see the online MATLAB C++ Math Library Reference.

Using Array Creation Routines

The MATLAB C++ Math Library provides routines that create commonly used MATLAB arrays, such as arrays of 0's or 1's.

When you call these routines, you define the number of dimensions of the array by the number of dimensions you specify as arguments. Given n arguments, the routines return a multidimensional array with the n dimensions. (The eye() routine and the magic() routine only support two dimensional arrays.)

For example, this code fragment creates a 2-by-3-by-2 array of normally distributed random numbers.

This code produces the following array:

Creating Integer Ramps.   

In MATLAB, the : (colon) operator can be used as a fast way to create a vector of monotonically increasing numbers. This capability is often used as a wildcard in MATLAB array indexing expressions. The MATLAB C++ Math Library uses two routines to emulates the : operator:

When you use ramp(), the first argument represents the starting value and the second argument represents the end value. As an example, the following code fragment creates a vector of all the numbers between 1 and 10.

The library also supports the three-argument form of ramp(), where the first argument represents the starting value, the second argument represents the size of the increment between values and the third argument.

Calling MATLAB Arithmetic Routines

As in MATLAB, most of the operators and functions in the MATLAB C++ Math Library create at least one new array as their result. For example, when you multiply two arrays, the result is a new array. This code demonstrates how multiplying a 4-by-4 array of 1's by the 4-by-4 identity matrix creates a new array, C.

C is a new array; the result of the multiplication.

Using Concatenation

Vertical and horizontal concatenation are useful ways to construct arrays of any size and shape. In MATLAB, the concatenation operator ([]) performs both operations. The MATLAB C++ Math Library uses two routines to emulate this operator:

Concatenating Horizontally.   

In MATLAB, you can horizontally concatenate the scalar arrays 1, 2, 3, 4, 5, and 6 into a vector containing one row and six columns.

You can create the same vector in C++ code using horzcat().

This code fragment produces this output:

Concatenating Vertically..   

To vertically concatenate the same scalar arrays into a 2-by-2 matrix in MATLAB, insert a semicolon in the list of arrays where you want to create rows:

To create this matrix in a C++ program, you must use vertcat(), using nested calls to horzcat() to create the rows.

This code fragment produces this output:

horzcat() and vertcat() work on vectors and two-dimensional arrays as well as scalars. For example, the following code fragment concatenates the two dimensional arrays A and B to create the two-dimensional array C.

Horizontally concatenated arrays must have the same number of rows; vertically concatenated arrays must have the same number of columns.

Using Concatenation to Create Arrays of More Than Two Dimensions.   

To create arrays of more than two-dimensions through concatenation, use the cat() routine. You cannot create arrays of more than two dimensions using horzcat() and vertcat().

In MATLAB, the cat function concatenates a group of arrays along a specified dimension using the following syntax

where A1, A2, and so on are the arrays to concatenate, and dim is the dimension along which to concatenate the arrays.

For example, this MATLAB code concatenates the two-dimensional arrays A and B into a three-dimensional array using the cat function.

This code fragment creates the same three-dimensional array in a C++ program:

This code produces the following output:

If the number of dimensions you specify in dim is greater than the number of arrays you specify as arguments, cat automatically adds subscripts of 1 between dimensions, if necessary. For example, if you change cat(3,A,B) to cat(4,A,B), the code produces the following output. Note the added dimension in the index subscripts.

Using Assignment

You can create scalar arrays using the C++ assignment (=) operator. For example, the following C++ code creates an array named A and assigns the value 5 to A.

The result of this assignment is a 1-by-1 array (one row, one column) containing the single number 5.0 represented in double-precision floating-point format.

You can assign a nonscalar value to a variable that contains a scalar array, or a scalar value to a variable that contains a nonscalar array. In both cases, the MATLAB C++ Math Library manages the memory associated with each array to ensure that there are no memory leaks.

You can also create string arrays using the C++ assignment operator. The following C++ code creates an array named A and assigns the value "abcd" to A.

Creating Multidimensional Arrays By Assignment.   

You can create multidimensional arrays using indexed assignment statements. You use MATLAB array indexing to specify a location in an array. MATLAB creates the array (or extends an existing array) to accommodate the location specified.

For example, the following assignment statement creates a new three dimensional array by assigning a single value to the location specified by row 2, column 2, page 2. MATLAB fills the elements of the array with zeros before making the assignment.

The MATLAB C++ Math Library supports this same syntax. You can create a multidimensional array by assigning a value to a location in the array. The library creates an array (or extends an existing array) to accommodate the location. The following C++ code fragment creates the same three dimensional array.


 Numeric Arrays Initializing a Numeric Array with Data