mex内存分配mxRealloc

时间:2012-07-21 12:03:59

标签: matlab memory-management mex realloc

我必须读取一个double矩阵,处理它的值并将它们插入一个新的矩阵中,其中一个维度在开头就不知道了。

在静态内存分配中,我的代码是:

#include <mex.h>

void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
  const mxArray *I = prhs[0];
  double *indata = mxGetPr(I);
  double *submtx = mxGetPr(prhs[1]);
  const int *size = mxGetDimensions(I);
  int xp = (int)submtx[0], yp = (int)submtx[1], zp = (int)submtx[2];
  int xi = size[0], yi = size[1], zi = size[2];

  int numsubmtx = (xi - xp + 1)*(yi - yp + 1)*(zi - zp + 1);
  int out_rows = xp*yp*zp;
  int out_cols = numsubmtx;
  mxArray *out = mxCreateDoubleMatrix( out_rows, out_cols, mxREAL );
  double *outdata = mxGetPr(out);

  int submtx_counter = 0;
  for( int z_offset = 0; ...; z_offset++ ){
        for( int y_offset = 0; ...; y_offset++ ){
              for( int x_offset = 0; ...; x_offset++ ){
                    int row = 0;
                    for( int z_counter = 0; ...; z_counter++ ){
                          for( int y_counter = 0; ...; y_counter++ ){
                                for( int x_counter = 0; ...; x_counter++ ){
                                      outdata[submtx_counter*out_rows + row] = 
                                            indata[ (x_offset+x_counter) + (y_offset+y_counter)*xi + (z_offset+z_counter)*xi*yi ];
                                      ++row;
                                }}}
              ++submtx_counter;
              }}}
  plhs[0] = out;
}

在动态版本中,我不知道out_cols的值,所以当满足*out值的条件时,我必须重新分配indata

我的想法是这样的:

#include <mex.h>

void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
  const mxArray *I = prhs[0];
  double *indata = mxGetPr(I);
  double *submtx = mxGetPr(prhs[1]);
  const int *size = mxGetDimensions(I);
  int xp = (int)submtx[0], yp = (int)submtx[1], zp = (int)submtx[2];
  int xi = size[0], yi = size[1], zi = size[2];

  int numsubmtx = (xi - xp + 1)*(yi - yp + 1)*(zi - zp + 1);
  int out_rows = xp*yp*zp;
  //int out_cols = numsubmtx; NOW UNKNOWN!
  mxArray *out = NULL;
  double *outdata = mxGetPr(out);

  int submtx_counter = 0;
  for( int z_offset = 0; ...; z_offset++ ){
        for( int y_offset = 0; ...; y_offset++ ){
              for( int x_offset = 0; ...; x_offset++ ){
                    int row = 0;
                    double condition=0;
                    for( int z_counter = 0; ...; z_counter++ ){
                          for( int y_counter = 0; ...; y_counter++ ){
                                for( int x_counter = 0; ...; x_counter++ ){
                                      condition += indata[ (x_offset+x_counter) + (y_offset+y_counter)*xi + (z_offset+z_counter)*xi*yi ]/(xp*yp*zp);
                                      ++row;
                                }}}
                    if(coundition>0.5){
                      out = mxRealloc(out, (submtx_counter+1)*out_rows*sizeof(double));
                      double *outdata = mxGetPr(out);
                      int row = 0;
                      for( int z_counter = 0; ...; z_counter++ ){
                          for( int y_counter = 0; ...; y_counter++ ){
                                for( int x_counter = 0; ...; x_counter++ ){
                                      outdata[submtx_counter*out_rows + row] = indata[ (x_offset+x_counter) + (y_offset+y_counter)*xi + (z_offset+z_counter)*xi*yi ];
                                      ++row;
                                }}}
              ++submtx_counter;
              }
              }}}
  plhs[0] = out;
}

有什么问题?

1 个答案:

答案 0 :(得分:1)

我没有仔细查看代码的逻辑,但我可以看到以下问题:

mxArray *out = NULL;
double *outdata = mxGetPr(out);

out = mxRealloc(out, (submtx_counter+1)*out_rows*sizeof(double));

会有奇怪的行为。 mxArray是一个结构,您希望为结构指向的数据而不是结构本身重新分配内存。相反,尝试

double *outdata = mxMalloc(out_rows*sizeof(double));

outdata = mxRealloc(outdata, (submtx_counter+1)*out_rows*sizeof(double));

然后在最后,创建输出矩阵:

mxArray *out = mxCreateDoubleMatrix(out_rows, submtx_counter, mxREAL);
mxSetPr(out, outdata);
plhs[0] = out;

这将保证out中的元数据为您提供正确大小的矩阵等。