Output a matrix with c++ and mex

后端 未结 2 992
我在风中等你
我在风中等你 2021-01-29 01:48

I have a problem with my c++ code. I want to return a matrix of k-dimensions from my cpp program to Matlab.

The matrix I want to pass is stored in all_data

2条回答
  •  终归单人心
    2021-01-29 02:20

    mxGetPr does not return a vector >. It returns a double *. MATLAB arrays are stored contiguously in memory, column-major. Assuming you've created plhs[0] with the correct dimensions, then all you need to do is this:

    double *indexes = mxGetPr(plhs[0]);
    for (int i=0; i < (npoints1+1); i++)
        for (int j=0; j < ndims1; j++)
            indexes[i + ndims1*j] = all_data[ i ][ j ];
    

    Note the conversion of the 2 indices to a linear offset.

提交回复
热议问题