转换向量<>到在C ++中的代码生成后的emxArray_real_T [英] Convert vector<> to emxArray_real_T in C++ after code-generation from MATLAB

查看:1916
本文介绍了转换向量<>到在C ++中的代码生成后的emxArray_real_T的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用MATLAB编码器将一些matlab代码转换为C ++。

I have converted some matlab code to C++ using the MATLAB coder.

我知道有一些类似的问题,但没有解释发现确实帮助我的问题,这有点不同。

I am aware that there are some questions similar to this one, but none of the explanations I found did help with my problem, which is somewhat different.

生成的C ++代码给我以下函数:

The generated code for C++ gives me the following function:

void getTargetPoint(const emxArray_real_T *eyeMatrix, const emxArray_real_T
                *HypAlpha, const emxArray_real_T *HypCov, const
                emxArray_real_T *HypMean, const double xs[4], double Ymu[3])

其中Ymu是我正在寻找的输出。但是,即使我在转换器中将我的输入定义为不确定大小的数组,它们也是我的代码中的向量的向量:

Where Ymu is the output I am looking for. However, even if I defined my inputs as arrays of undetermined size in the converter, they are vectors of vectors in my code:

eyeMatrix = vector<vector <double>>  

它们都有不同的尺寸,例如eyeMatrix = [968x4],但在其他情况下可以是[anything x 4]

They all have different dimensions, for instance, eyeMatrix = [968x4], but in any other case it could be [anything x 4]

我尝试了很多东西,但是我不能把我的输入数据(向量的向量)转换成类型:emxArray_real_T

I have tried many things, but I cannot manage to convert my input data (vector of vectors) into type: emxArray_real_T

有没有反正呢?或者我应该将我的输入数据转换成别的,然后进入emxArray_real_T?

Is there anyway to do this? Or should I convert my input data into something else and then into emxArray_real_T?

任何帮助或想法都非常感谢!

Any help or idea is greatly appreciated!

推荐答案

假设 eyeMatrix 是一个矩形矩阵(即每个内部向量具有相同的长度),因为生成的代码可能意味着与非乱糟糟的数组一起工作。

This answer assumes that eyeMatrix is a rectangular matrix (i.e. each inner vector has the same length) since the generated code likely is meant to work with non-ragged arrays.

您可以查看:

http://stackoverflow.com/a/24271438/3297440

文档了解如何使用 emxArray * 数据类型。

and the documentation for an overview of working with emxArray* data types.

总结答案,你会在文件中声明函数< name> _emxAPI.h 看起来像:

To summarize the answer, you'll have functions declared in the file <name>_emxAPI.h that look like:

extern emxArray_real_T *emxCreateWrapper_real_T(double *data, int rows,
                                                int cols);
extern emxArray_real_T *emxCreate_real_T(int rows, int cols);

你会注意到第一个需要一个 double *

You'll notice that the first takes a double *. This should be a pointer to your data stored in column-major order.

由于 eyeMatrix 可能是以row-major顺序存储的,主要顺序并且可能不是实际连续的(我不知道保证嵌套 std :: vectors 导致连续数据)第一步可以调用 emxCreate_real_T

Since eyeMatrix is likely in row-major order and may not actually be contiguous (I'm not aware of a guarantee that nested std::vectors result in contiguous data) a first step can be to call emxCreate_real_T with the proper dimensions.

这会分配并返回一个 emxArray_real_T 我们可以调用 emx emx-> size 将有2个元素,这是您设置的尺寸。字段 emx-> data 将是指向 rows * cols 值的指针( double 值)。然后,您可以以列为主的方式将数据从 eyeMatrix 复制到 emx-> data 。所以如果你的矩阵是:

This allocates and returns an emxArray_real_T which we can call emx. emx->size will have 2 elements which are the dimensions you set. The field emx->data will be a pointer to rows*cols values (double values in your case). Then you can just copy the data from eyeMatrix to emx->data in a column-major fashion. So if your matrix is:

1, 2
3, 4

emx-> data 中的数据应为:

1, 3, 2, 4

从这里,只需传递 emx 作为输入。

From there, just pass emx as the input.

最后,不要忘记调用在完成 emxArray_real_T 值后,可以使用 emxDestroyArray * 函数清除任何分配的内存。

Lastly, don't forget to call the proper emxDestroyArray* functions to clean up any allocated memory when you are done with the emxArray_real_T values.

这篇关于转换向量&lt;&gt;到在C ++中的代码生成后的emxArray_real_T的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆