c++ - 将此 C++ 代码转换为 Matlab

标签 c++ matlab matrix sparse-matrix

我正在尝试将直接的 C 代码转换为 matlab,但我被困在一开始似乎很清楚的地方。这个板 block 是做什么的? SIZE_N2 = 25, w = 533, h = 800

//Init the L matrix. L is a sparse matrix that only contains SIZE_N2 non-zero elements per row.
//We store the L matrix in an array with one row per image pixel and one column per non-zero value. 
//Each array cell accumulates values according to equation 11.
LMatrix = new double* [w*h];
for(i=0; i<w*h; i++){
    LMatrix[i] = new double[SIZE_N2];
    for(j=0; j<SIZE_N2; j++){
        LMatrix[i][j] = 0;
    }
}

不是在Matlab中创建这个矩阵吗?

LMatrix = zeros(SIZE_N2, w*h);

当我用它运行代码时,矩阵在 for 循环中越界。

有人知道这个的正确实现方式吗?

谢谢!

最佳答案

首先,引用整个 source code 会很有帮助(以防万一)和一些documentation about it (所以我们知道,例如,什么是“方程式 11”)。

现在,如果我正确理解了代码,那么在 MATLAB 中所有内容(包括循环)都可以替换为以下内容:

LMatrix = sparse([],[],[],w*h,w*h,SIZE_N2*h);

除非您事先知道非零元素应该在哪里,在这种情况下,您可以直接在那里构建最终的稀疏矩阵,然后使用其他语法之一( docs1docs2 )。

关于c++ - 将此 C++ 代码转换为 Matlab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30389249/

相关文章:

algorithm - 如何优化多(矩阵)开关/案例算法?

c++ - 在C++中使用std::thread和std::condition_variable进行自定义后台任务的SIGABRT

matlab - calcCovarMatrix 的问题

Matlab log(1) 并不总是零

c - 如何连接数字字符串然后将其分配给 'long' 类型变量?

python - 转置一个已经展平的方阵

c++ - std::unique_ptr 干扰某些 sf::RenderWindow 函数?

c++ - 为什么主可执行文件和 dlopen 加载的共享库共享一个命名空间静态变量的拷贝?

c++ - Pthread实例变量运行类中的方法

matlab2tikz 生成繁重的排版 tikz 代码