MATLAB Application Program Interface Reference | Help Desk |
mxSetM
Set the number of rows
#include "matrix.h" void mxSetM(mxArray *array_ptr, int m);m array_ptr Call
mxSetM
to set the number of rows in the specified mxArray
. The term "rows" means the first dimension of an mxArray
, regardless of the number of dimensions. Call mxSetN
to set the number of columns.
You typically use mxSetM
to change the shape of an existing mxArray
. Note that mxSetM
does not allocate or deallocate any space for the pr
, pi
, ir
, or jc
arrays. Consequently, if your calls to mxSetM
and mxSetN
increase the number of elements in the mxArray
, then you must enlarge the pr
, pi
, ir
, and/or jc
arrays. Call mxRealloc
to enlarge them. (See the mxSetN
reference page for an example of doing this.)
If your calls to mxSetM
and mxSetN
end up reducing the number of elements in the array, then you do can optionally reduce the sizes of the pr
, pi
, ir
, and/or jc
arrays in order to use heap space more efficiently.
Reshape a 3-by-2 mxArray
into a 6-by-1 mxArray
for more efficient use of memory without changing the data held by the mxArray
.
static double real_data[] = {5.2, 7.8, 4.3, 9.3, 8.2, 7.1}; int old_rows = 3, old_cols = 2; int new_rows = 6, new_cols = 1; double *pr; mxArray *array_ptr; /* Create a 3-by-2 array named "Apricot" */ array_ptr = mxCreateDoubleMatrix(old_rows, old_cols, mxREAL); pr = mxGetPr(array_ptr); memcpy((void *)pr,(const void *)real_data, old_rows*old_cols*sizeof(double)); mxSetName(array_ptr, "Apricots"); ... /* Change the dimensions of "Apricots" from 3-by-2 to 6-by-1. */ mxSetM(array_ptr, new_rows); mxSetN(array_ptr, new_cols); ...The data in
Apricot
starts out as
5.2 9.3 7.8 8.2 4.3 7.1After reshaping,
Apricot
's data becomes
5.2 7.8 4.3 9.3 8.2 7.1For an additional example, see
mxSetM.c
in the mx
subdirectory of the examples
directory.
mxGetM
, mxGetN
, mxSetN