Using the C Math Library | ![]() ![]() |
Determining Array Size
To determine the size of an array, use the mlfSize()
routine. The mlfSize()
routine returns a row vector containing the dimensions of the array. This code example creates a 2-by-3-by-2 array and displays the size vector returned by mlfSize()
.
mxArray *A = NULL; mxArray *dims = NULL; /* Create three-dimensional array */ mlfAssign(&A, mlfOnes(mlfScalar(2), mlfScalar(3), mlfScalar(2), NULL)); /* Determine size of array */ mlfAssign(&dims, mlfSize(NULL,A,NULL)); /* Display size vector */ mlfPrintMatrix(mxCreateString("The size of the array is \n")); mlfPrintMatrix(dims); mxDestroyArray(A); mxDestroyArray(dims);
This code produces the following output:
The size of the array is 2 3 2
Length of a Single Dimension
You can also get the size of one particular dimension of an array by specifying the dimension as an input argument. To get the length of the longest dimension of a multidimensional array, use the mlfLength()
routine. You can also use this routine to determine the size of a vector.
Dimension Return Values
The mlfSize()
routine can optionally return each dimension in a separate array. You specify these arrays as output arguments passed to the mlfVarargout()
routine. For more information about calling library routines that take variable number of input and output arguments, see Chapter 6.
The following code returns the dimensions in three output arguments: dim1
, dim2
, and dim3
. When used with output arguments, you do not need to bind the return value from mlfSize()
to a variable. The routine binds the return values to the output arguments specified. As with all bound arrays, you must explicitly free them.
mlfSize(mlfVarargout(&dim1,&dim2,&dim3,NULL), C, NULL); mxDestroyArray(dim1); mxDestroyArray(dim2); mxDestroyArray(dim3);
If the array has more dimensions than the number of output arguments specified, the last output argument contains the product of the remaining dimensions.
![]() | Determining Array Type | Determining Array Shape | ![]() |