Using the C++ Math Library | ![]() ![]() |
Array Size
In MATLAB, the size()
function returns the size of an array as an array. The MATLAB C++ Math Library provides a corresponding version of size()
that also returns an array. Because this C++ version allocates an array to hold just two integers, it is not efficient. The mwArray
Size
member functions below return the size of an array more efficiently.
An array (a matrix is a special case) has two sizes: the number of its dimensions (for matrices, always two) and the actual size of each dimension. You can use these Size()
functions to determine both the number of dimensions and the size of each dimension:
int32 Size() const
Return the number of dimensions.
int32 Size(int32 dim) const
Return the size (number of elements) of the indicated dimension.
int32 Size(int32* dims, int maxdims=2) const
Determine the sizes of all the dimensions of the array and return them via the given integer array, dims
. maxdims
is the maximum number of dimensions the function should return. The input integer array dims
must contain enough space to store at least maxdims
integers. If maxdims
is less then the number of dimensions of the mxArray
, the last dimension returned is the product of the remaining dimensions. This function's return value is the number of dimensions of the array.
For example, this code demonstrates the difference in efficiency between one of the mwArray
Size
member functions and the nonmember function.
int32 dims[2]; mwArray mat = rand(4,4); mwArray sz; // Use one of the Size member functions. // Requires 8 bytes to return two integers, 4 and 4. No memory is // dynamically allocated. mat.Size(dims); // Use the library's size function. // Requires dynamic memory allocation of at least 85 bytes for // the same two integers: 10 times more space, plus the // inefficiency of data access (via pointers). sz = size(mat);
![]() | Operators | Extracting Data from an mwArray | ![]() |