Using the C++ Math Library | ![]() ![]() |
Determining Array Size
You can determine the size of an array in several ways:
size()
routine, which returns the number of rows and columns in an arraysize()
that returns integersmwArray::Size()
member functionmwArray::EltCount()
member functionUsing the size() Routine
mwArray A = rand(4,7) ; mwArray m, n; size(mwVarargout(m,n), A);
This version of size()
returns the number of rows and columns in one or more separate arrays. Because it returns the dimensions as MATLAB scalar arrays, size()
is consistent with the rest of the interface. However, because it returns arrays, it is far slower and uses more storage than the overloaded version of size()
that returns dimensions as integers.
Using the Overloaded size() Routine
mwArray A = rand(4,7); int m, n; m = size(&n, A);
The overloaded version of size()
returns array dimensions as integers rather than scalar arrays. This version of size()
is efficient and easy to use; however, it only supports two dimensional arrays. It has been superseded by the member function, mwArray::Size()
, which works for multidimensional arrays.
Using the mwArray Size() Member Functions
The mwArray
object supports several member functions that returns array dimensions as integers rather than scalar arrays. These member functions are the most efficient way to compute the dimensions of an array.
The following examples show the various ways to determine array dimensions using these member functions.
int32 dims[2]; int32 ndims[3]; mwArray A = rand(4,7); // Two-dimensional array mwArray B = rand(4,7,4); // Three-dimensional array A.Size(dims); // Sets dims to (4, 7), maxdims defaults to 2 B.Size(ndims,3); // Sets ndims to (4, 7, 4) A.Size(); // Returns the number of dimensions: 2 A.Size(1); // Returns the size of the 1st dimension: 4 A.Size(2); // Returns the size of the 2nd dimension: 7
mwArray EltCount() Member Function
In addition to the size member functions, the mwArray object includes a member function, named EltCount()
, that returns the number of elements in the array, determined by the product of the length of each dimension:
A.EltCount(); // Returns the product of M and N: 28
Note The capitalization of these function names is significant; C++ function names are case-sensitive. |
![]() | Converting Data to MATLAB Arrays | Indexing into Arrays | ![]() |