如何转换浮动[] []数组类型到" emxArray_real_T * X" [英] How to convert float[][] type array to "emxArray_real_T *x"

查看:2175
本文介绍了如何转换浮动[] []数组类型到" emxArray_real_T * X"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经转换函数,它接受一个N×N的矩阵作为输入,并提供了从MATLAB矩阵输出的N×N至C,使用MATLAB $ C $铬。
它给了我这即有两个参数的函数

 无效FUNC(常量emxArray_real_T * X,emxArray_real_T * Y)

我得到x是输入到函数和我可以从y中的函数的输出。
问题是我有浮动[] []数组一个,我希望给这个数据作为输入FUNC,而只需要emxArray_real_T * X为输入。

这是如何将这个浮点数据转换[] [],以emxArray_real_T任何想法* X

emxArray_real_T具有这种结构

 结构emxArray_real_T
{
    real_T *数据;
    int32_T *的大小;
    int32_T allocatedSize;
    int32_T numDimensions;
    boolean_T canFreeData;
};


解决方案

我发现这以后,我们可以使用这些以下步骤创建任何数据到emxArray_real_T * ...

第1步:比方说你有一个浮动或双或其他任何一个数组......这是2维..它可以是任何尺寸...它可以是3 ..它可以是4。 ..
现在第一步是声明

  emxArray_real_T * X,* Y;

其中x是你的输入,你会得到y中的输出...

到现在这个时候,我们只声明的变量并没有设置任何尺寸或他们的任何大小...

第2步:在同一个文件作为你的函数,你要调用应该有这些功能..如果没有,那么你必须声明它们....

 静态无效emxInit_real_T(emxArray_real_T ** pEmxArray,int32_T numDimensions)
 {
   emxArray_real_T * emxArray;
   int32_T我;
   * pEmxArray =(emxArray_real_T *)malloc的(的sizeof(emxArray_real_T));
   emxArray = * pEmxArray;
   emxArray->数据=(real_T *)NULL;
   emxArray-> numDimensions = numDimensions;
   emxArray->大小=(int32_T *)malloc的((uint32_t的)(的sizeof(int32_T)* numDimensions));
   emxArray-> allocatedSize = 0;
   emxArray-> canFreeData = TRUE;
   对于(i = 0; I< numDimensions;我++){
     emxArray->尺寸由[i] = 0;
   }
 }

现在可以初始化的变量X,Y为以下

  emxInit_real_T(放大器,X,2);
emxInit_real_T(安培; Y,2);

在这里2是我们希望为输入或输出矩阵的尺寸。

第三步:对我来说,X的尺寸为2所以我会做什么是这个样子
说我的输入为n×n矩阵,所以我会做到这一点。

  X轴和GT;大小[0] = N;
X-GT&;大小[1] = N;

y与此类似。

  Y-GT;大小[0] = N;
Y-GT;大小[1] = N;

如果你有3个或4个维度,那么你可以把更像是X->尺寸[2] =该尺寸..等..

第四步:现在我们已经确保程序下发指定的内存为这些变量所以应该有像下面的函数..如果不是那么你必须声明它...

 静态无效emxEnsureCapacity(emxArray__common * emxArray,int32_T oldNumel,
  int32_T elementSize)
{
  int32_T newNumel;
  int32_T我;
  无效* newData;
  newNumel = 1;
  对于(i = 0; I< emxArray-> numDimensions;我++){
    newNumel * = emxArray->大小[I]
  }  如果(newNumel> emxArray-> allocatedSize){
    I = emxArray-> allocatedSize;
    如果(ⅰ&下; 16){
      我= 16;
    }    而(I< newNumel){
      I&所述;&下; = 1;
    }    newData =释放calloc((uint32_t的)我,(uint32_t的)elementSize);
    如果(emxArray->数据!= NULL){
      的memcpy(newData,emxArray->数据,(uint32_t的)(elementSize * oldNumel));
      如果(emxArray-> canFreeData){
        免费(emxArray->数据);
      }
    }    emxArray->数据= newData;
    emxArray-> allocatedSize = I;
    emxArray-> canFreeData = TRUE;
  }
}

这应该是结构名称emxArray__common像下面如果没有的话就声明它像下面

 结构emxArray__common
{
  void *的数据;
  int32_T *的大小;
  int32_T allocatedSize;
  int32_T numDimensions;
  boolean_T canFreeData;
};
typedef结构emxArray__common emxArray__common;

第五步:然后做这样的..

  emxEnsureCapacity((emxArray__common *)X,0,(int32_T)的sizeof(real_T));
emxEnsureCapacity((emxArray__common *)Y,0,(int32_T)的sizeof(real_T));

现在这两个可变因素在他们指定的n * n的内存。
现在输入的数据将被保存在排明智的将被保存在它的X ...数据..

  X轴和GT;数据[I * N + J] = input_data [I] [J]。

如果你明白我被它的意思。
现在你可以传递你要调用,然后返回的数据将Y轴的功能x和y,这将是逐行太..所以请仔细阅读并疗法已经将它...你已经执行在C MATLAB函数....

I have converted a function which takes a NxN matrix as input and gives a NxN matrix output from matlab to C, using the MatlabCoder. It gave me the function which has two parameters namely

void func(const emxArray_real_T *x, emxArray_real_T *y)

I get that x is the input to the function and i can get the output of the function from y. The problem is i have a array in float[][] and i wish to give this data as an input to the func, which only takes emxArray_real_T *x as an input.

Any ideas on how to convert this float[][] data to emxArray_real_T *x

emxArray_real_T has this structure

struct emxArray_real_T
{
    real_T *data;
    int32_T *size;
    int32_T allocatedSize;
    int32_T numDimensions;
    boolean_T canFreeData;
};

解决方案

I found this later that we can create any data into the emxArray_real_T * using these following steps...

Step 1: Lets say you have a array with float or double or anything else... it is of 2 dimensions.. it can be of any dimensions... it can be 3.. it can be of 4... now first step is to declare

emxArray_real_T *x, *y;

where x will be your input and you will get the output in y...

now till this time we have only declared the variables and not set any dimensions or any size in them...

Step 2: In the same file as your function that you want to call there should be these functions.. if not then you must declare them....

 static void emxInit_real_T(emxArray_real_T **pEmxArray, int32_T numDimensions)
 {
   emxArray_real_T *emxArray;
   int32_T i;
   *pEmxArray = (emxArray_real_T *)malloc(sizeof(emxArray_real_T));
   emxArray = *pEmxArray;
   emxArray->data = (real_T *)NULL;
   emxArray->numDimensions = numDimensions;
   emxArray->size = (int32_T *)malloc((uint32_T)(sizeof(int32_T) * numDimensions));
   emxArray->allocatedSize = 0;
   emxArray->canFreeData = TRUE;
   for (i = 0; i < numDimensions; i++) {
     emxArray->size[i] = 0;
   }
 }

now you can initialize your variables "x,y" as following

emxInit_real_T(&x, 2);
emxInit_real_T(&y, 2);

here 2 is the dimension of the matrix that we want as input or output..

Step 3: In my case the dimension of x is 2 so what i will do is like this Say my input is n*n matrix so i will do this

x->size[0]=n;
x->size[1]=n;

and similarly for y

y->size[0]=n;
y->size[1]=n;

if you have 3 or 4 dimension then you can put more like x->size[2]=that size.. and so on..

Step 4: Now we have ensure that the program allots the specified memory to these variables so for that there should be a function like below.. if not then your must declare it...

static void emxEnsureCapacity(emxArray__common *emxArray, int32_T oldNumel,
  int32_T elementSize)
{
  int32_T newNumel;
  int32_T i;
  void *newData;
  newNumel = 1;
  for (i = 0; i < emxArray->numDimensions; i++) {
    newNumel *= emxArray->size[i];
  }

  if (newNumel > emxArray->allocatedSize) {
    i = emxArray->allocatedSize;
    if (i < 16) {
      i = 16;
    }

    while (i < newNumel) {
      i <<= 1;
    }

    newData = calloc((uint32_T)i, (uint32_T)elementSize);
    if (emxArray->data != NULL) {
      memcpy(newData, emxArray->data, (uint32_T)(elementSize * oldNumel));
      if (emxArray->canFreeData) {
        free(emxArray->data);
      }
    }

    emxArray->data = newData;
    emxArray->allocatedSize = i;
    emxArray->canFreeData = TRUE;
  }
}

for this there should be struct names emxArray__common like below if not then do declare it like below

struct emxArray__common
{
  void *data;
  int32_T *size;
  int32_T allocatedSize;
  int32_T numDimensions;
  boolean_T canFreeData;
};
typedef struct emxArray__common emxArray__common;

Step 5: then do like this..

emxEnsureCapacity((emxArray__common *)x, 0, (int32_T)sizeof(real_T));
emxEnsureCapacity((emxArray__common *)y, 0, (int32_T)sizeof(real_T));

now both these varibles have the specified n*n memory in them. now the input data will be saved in the data of x... that will be saved in it row wise..

x->data[i*n+j]=input_data[i][j];

if you get what i mean by it. now you can pass the x and y in the function that you want to call and then the data returned will be in y and it will be row-wise too.. so read it carefully and ther you have it... you have executed the matlab function in c....

这篇关于如何转换浮动[] []数组类型到&QUOT; emxArray_real_T * X&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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