MATLAB Application Program Interface Reference | Help Desk |
The MATLAB Array
The MATLAB language works with only a single object type -- the MATLAB array. All MATLAB variables, including scalars, vectors, matrices, strings, cell arrays, and structures are stored as MATLAB arrays. ThemxArray
declaration corresponds to the internal data structure that MATLAB uses to represent arrays. The MATLAB array is the C language definition of a MATLAB variable. This mxArray
structure contains, among other things:
mxArray
structure contains parameters called pr
and pi
. pr
contains the real part of the matrix data; pi
contains the imaginary data, if there is any. Both pr
and pi
are one-dimensional arrays of double-precision numbers. The elements of the matrix are stored in these arrays columnwise. This is how Fortran stores matrices; MATLAB uses this convention because it was originally written in Fortran.
Sparse matrices have a different storage convention in MATLAB. The parameters pr
and pi
are still arrays of double-precision numbers, but there are now three new parameters, nzmax
, ir
, and jc
:
nzmax
is an integer that contains the length of ir, pr, and if it exists, pi. It is the maximum number possible number of nonzero elements in the sparse matrix.
ir
points to an integer array of length nzmax
containing the row indices of the corresponding elements in pr
and pi
.
jc
points to an integer array of length N+1
that contains column index information. For j
in the range 0
j
N-1
, jc[j]
is the index in ir
, pr
(and pi
if it exists) of the first nonzero entry in the j
th column and jc[j+1]-1
index of the last nonzero entry. As a result, jc[N]
is also equal to nnz
, the number of nonzero entries in the matrix. If nnz
is less than nzmax
, then more nonzero entries can be inserted in the array without allocating additional storage.