MATLAB Application Program Interface Reference | Help Desk |
mxSetNzmax
Set the storage space for nonzero elements
#include "matrix.h" void mxSetNzmax(mxArray *array_ptr, int nzmax);array_ptr nzmax
The number of elements that mxCreateSparse
should allocate to hold the arrays pointed to by ir
, pr
, and pi
(if it exists). You should set nzmax
greater than or equal to the number of nonzero elements in the mxArray
, but you should set it to be less than or equal to the number of rows times the number of columns. If you specify an nzmax
value of 0, mxSetNzmax
sets the value of nzmax
to 1.
mxSetNzmax
to assign a new value to the nzmax
field of the specified sparse mxArray
. The nzmax
field holds the maximum possible number of nonzero elements in the sparse mxArray
.
The number of elements in the ir
, pr
, and pi
(if it exists) arrays must be equal to nzmax
. Therefore, after calling mxSetNzmax
, you must change the size of the ir
, pr
, and pi
arrays. To change the size of one of these arrays, you should
mxCalloc
, setting n
to the new value of nzmax
.
memcpy
to copy the contents of the old array to the new area allocated in Step 1.
mxFree
to free the memory occupied by the old array.
mxSet
routine (mxSetIr
, mxSetPr
, or mxSetPi
) to establish the new memory area as the current one.
nzmax
be? One philosophy is that you should set nzmax
equal to or slightly greater than the number of nonzero elements in a sparse mxArray
. This philosophy conserves precious heap space. Another philosophy is that you should make nzmax
equal to the total number of elements in an mxArray
. This philosophy eliminates (or, at least reduces) expensive reallocations.
Create a sparse mxArray
, then reduce its nzmax
value to conserve heap space:
int starting_nzmax=5000, rows=2500, cols=2, new_nzmax=4; static double static_pr_data[] = {5.8, 6.2, 5.9, 6.1}; static int static_ir_data[] = {0, 1, 1, 2}; static int static_jc_data[] = {0, 2, 4}; double *start_of_pr, *new_pr_ptr; int *start_of_ir, *start_of_jc, *new_ir_ptr; mxArray *array_ptr; /* Create a sparse array and name it "Sparrow". */ array_ptr = mxCreateSparse(rows, cols, starting_nzmax, mxREAL); mxSetName(array_ptr, "Sparrow"); /* Place pr data into Sparrow. */ start_of_pr = (double *)mxGetPr(array_ptr); memcpy(start_of_pr, static_pr_data, sizeof(static_pr_data)); /* Place ir data into Sparrow. */ start_of_ir = (int *)mxGetIr(array_ptr); memcpy(start_of_ir, static_ir_data, sizeof(static_ir_data)); /* Place jc data into Sparrow. */ start_of_jc = (int *)mxGetJc(array_ptr); memcpy(start_of_jc, static_jc_data, sizeof(static_jc_data)); ... /* Decrease the size of nzmax. */ mxSetNzmax(array_ptr, new_nzmax); /* Adjust ir accordingly. */ new_ir_ptr = mxCalloc(new_nzmax, sizeof(double)); memcpy(new_ir_ptr, mxGetIr(array_ptr), new_nzmax*sizeof(double)); mxFree(mxGetIr(array_ptr)); mxSetIr(array_ptr, new_ir_ptr); /* Adjust pr accordingly. */ new_pr_ptr = mxCalloc(new_nzmax, sizeof(double)); memcpy(new_pr_ptr, mxGetPr(array_ptr), new_nzmax*sizeof(double)); mxFree(mxGetPr(array_ptr)); mxSetPr(array_ptr, new_pr_ptr);There is no need to adjust
pi
because Sparrow
is purely real. For an additional example, see mxSetNzmax.c
in the mx
subdirectory of the examples
directory.
mxGetNzmax