You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现Eigen向量与MATLAB mex cell数组的指针式数据同步传递?

Solution: Map Eigen Vectors Directly to MATLAB mex Cell Array Memory

Got it, let's fix this issue. The core problem here is that you're trying to assign an Eigen::Map to an Eigen::VectorXd object, which won't share memory—instead, you need to use an array of Eigen::Map pointers to directly wrap the MATLAB mex array memory. This way, any modifications to the Eigen objects will immediately reflect in the y_output cell array without extra copies.

Here's the corrected code with explanations:

#include "mex.h"
#include "matrix.h"
#include <Eigen>

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
    // prhs[0]: a cell array of length T, each element is a vector with different lengths
    mwSize T = mxGetNumberOfElements(prhs[0]);
    mwSize* n = new mwSize[T];
    Eigen::VectorXd* z = new Eigen::VectorXd[T];

    // Process input cell array (this part was fine)
    for(int t=0; t<T; t++){
        mxArray* input_cell = mxGetCell(prhs[0], t);
        n[t] = mxGetNumberOfElements(input_cell);
        // Map input vector to Eigen (read-only or read-write, depending on your needs)
        z[t] = Eigen::Map<Eigen::VectorXd>(mxGetPr(input_cell), n[t]);
    }

    // Create output cell array
    mxArray* y_output = mxCreateCellMatrix(T, 1);
    // Use an array of Eigen::Map pointers to wrap mex array memory
    Eigen::Map<Eigen::VectorXd>* y = new Eigen::Map<Eigen::VectorXd>[T];

    for(int t=0; t<T; t++){
        // Create a MATLAB double vector in the cell
        mxArray* output_vec = mxCreateDoubleMatrix(n[t], 1, mxREAL);
        mxSetCell(y_output, t, output_vec);

        // Directly map the mex vector's memory to an Eigen::Map
        // This shares memory—changes to y[t] will update output_vec (and y_output)
        y[t] = Eigen::Map<Eigen::VectorXd>(mxGetPr(output_vec), n[t]);
        
        // Optional: Initialize to zero (you can do this via Eigen directly)
        y[t].setZero();
    }

    // Now call your custom function—modifications to y[t] will auto-update y_output
    // Myfun(y, z);

    // Assign output
    plhs[0] = y_output;

    // Clean up allocated memory to avoid leaks
    delete[] z;
    delete[] y;
    delete[] n;
}

Key Fixes & Explanations:

  • Change y to an array of Eigen::Map pointers: Instead of Eigen::VectorXd* y, we use Eigen::Map<Eigen::VectorXd>* y. Eigen::Map is a lightweight wrapper that doesn't allocate its own memory—it directly uses the memory of the MATLAB mex array. This is the critical part for shared memory access.
  • Properly bind each y[t] to the mex array memory: After creating output_vec with mxCreateDoubleMatrix, we wrap its underlying data pointer (mxGetPr(output_vec)) with Eigen::Map. Any writes to y[t] (e.g., y[t](0) = 5.0) will directly modify the memory of output_vec, which is part of y_output.
  • Memory cleanup: Don't forget to delete the dynamically allocated arrays (z, y, n) to prevent memory leaks in your mex function.

Important Notes:

  • Ensure that the MATLAB mex arrays (output_vec) are created before creating the Eigen::Map—the memory must exist and be valid for the Map to work.
  • Eigen::Map doesn't take ownership of the memory, so you don't have to worry about it deleting the mex arrays (MATLAB manages those once you return y_output as a plhs).
  • If your Myfun expects Eigen::VectorXd* instead of Eigen::Map<Eigen::VectorXd>*, you can adjust it—since Eigen::Map inherits from Eigen::VectorXd, it should be compatible (Eigen's API treats Maps the same as regular Vectors for most operations).

内容的提问来源于stack exchange,提问作者Bayes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:21:16