【问题标题】:Initializing main function from Matlab Coder从 Matlab Coder 初始化主函数
【发布时间】:2017-07-25 20:46:49
【问题描述】:

我通过 Matlab 编码器创建了一个名为implicit_enumeration.cpp 的 C++ 程序,它以矩阵 A 和向量 b 作为输入并返回向量 zstar 和概率值 Vstar。我现在正在努力初始化 main 函数:

// Include Files
#include "stdafx.h"
#include "implicit_enumeration.h"
#include "main.h"

// Function Declarations
static void argInit_4x1_real_T(double result[4]);
static void argInit_4x9_real_T(double result[36]);
static void main_implicit_enumeration();

// Function Definitions

static void argInit_4x1_real_T(double result[4])
{
    int idx0;

    // Loop over the array to initialize each element.
    for (idx0 = 0; idx0 < 4; idx0++) {
        // Set the value of the array element.
        // Change this value to the value that the application requires.
        result[idx0] = 1;
    }
}

static void argInit_4x9_real_T(double result[36])
{
    int idx0;
    int idx1;

    // Loop over the array to initialize each element.
    for (idx0 = 0; idx0 < 4; idx0++) {
        for (idx1 = 0; idx1 < 9; idx1++) {
            // Set the value of the array element.
            // Change this value to the value that the application requires.
            if (idx0 == 0) {
                if (idx1 == 0) { result[idx0 + (idx1 << 2)] = 1; }
                else if (idx1 == 1) { result[idx0 + (idx1 << 2)] = 1; }
                else { result[idx0 + (idx1 << 2)] = 0; }
            }
            else if (idx0 == 1) {
                if (idx1 == 2) { result[idx0 + (idx1 << 2)] = 1; }
                else if (idx1 == 3) { result[idx0 + (idx1 << 2)] = 1; }
                else if (idx1 == 4) { result[idx0 + (idx1 << 2)] = 1; }
                else { result[idx0 + (idx1 << 2)] = 0; }
            }
            else if (idx0 == 2) {
                if (idx1 == 5) { result[idx0 + (idx1 << 2)] = 1; }
                else { result[idx0 + (idx1 << 2)] = 0; }
            }
            else {
                if (idx1 == 6) { result[idx0 + (idx1 << 2)] = 1; }
                else if (idx1 == 7) { result[idx0 + (idx1 << 2)] = 1; }
                else if (idx1 == 8) { result[idx0 + (idx1 << 2)] = 1; }
                else { result[idx0 + (idx1 << 2)] = 0; }
            }
        }
    }
}

static void main_implicit_enumeration()
{
    double dv0[36];
    double dv1[4];
    double zstar[9];
    double Vstar;

    // Initialize function 'implicit_enumeration' input arguments.
    // Initialize function input argument 'A'.
    // Initialize function input argument 'b'.
    // Call the entry-point 'implicit_enumeration'.
    argInit_4x9_real_T(dv0);
    argInit_4x1_real_T(dv1);
    implicit_enumeration(dv0, dv1, zstar, &Vstar);
}

int main(int argc, const char * const argv[])
{
    // Initialize the application.
    // You do not need to do this more than one time.
    // Invoke the entry-point functions.
    // You can call entry-point functions multiple times.
    main_implicit_enumeration();
    return 0;
}

具体来说,我想知道如何将 argv[] 初始化为矩阵 A 和向量 b。即使我已经通过函数 argInit_4x9_real_T() 和 argInit_4x1_real_T() 初始化了矩阵 A 和向量 b,是否有必要设置命令参数?如果是,如何包含矩阵和向量作为命令参数?我检查的示例始终显示单个整数或实数值,但不显示矩阵或向量。 提前谢谢!

【问题讨论】:

  • argv[] 未在代码中使用。不清楚“初始化主函数”是什么意思

标签: c++ matlab command-line-arguments


【解决方案1】:

实际上,你已经完成了。

在 main(int argc, const char * const argv[]): argc 是命令行参数的个数,argv 包含这些命令行参数。

示例: 如果调用test.ext something,argc 为 2,argv[0] 为“test.exe”,argv[1] 为“something”。

您的初始化在 main_implicit_enumeration() 中完成

  • dv0 是矩阵 A(4*9 矩阵 -> 36 个元素),它是 在 argInit_4x9_real_T() 中初始化
  • dv1 是你的向量 b(4 个向量 -> 4 个元素),它在 argInit_4x1_real_T 中初始化
  • zstar 是返回向量
  • Vstar 斯卡拉回归

问题是:您希望如何将值传递给您的程序?从文件中加载值?使用test.ext A=1,2,3;4,5,6 b=8,9 之类的格式?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多