使用c++ mex函数从matlab中获取输入参数
getting input parameters from matlab using C++ mex Function
double learning_rate = 1;
int training_epochs = 1;
int k = 1;
int train_S = 6;
int test_S = 6;
int visible_E = 6;
int hidden_E = 6;
// training data
int train_X[6][6] = {
{1, 1, 1, 0, 0, 0},
{1, 0, 1, 0, 0, 0},
{1, 1, 1, 0, 0, 0},
{0, 0, 1, 1, 1, 0},
{0, 0, 1, 1, 1, 0},
{0, 0, 1, 1, 1, 0}
};
上面的代码是输入参数我给我的函数。但是我想把它们转换成我的mexFunction中的一个函数,然后简单地调用它们。matlab端有如下
clear *
close all
clc
%% Load the data
X= [ 1, 1, 1, 0, 0, 0; ...
1, 0, 1, 0, 0, 0; ...
1, 1, 1, 0, 0, 0; ...
0, 0, 1, 1, 1, 0; ...
0, 0, 1, 1, 1, 0; ...
0, 0, 1, 1, 1, 0];
%% Define Parameters
numHiddenUnits = 6;
numIterations = 1000;
kCD = 1;
%% Compute the RBM
x = RBM(X, numHiddenUnits, numIterations, kCD);
标量输入参数相当简单。矩阵输入有点棘手,因为它们使用老式的Fortran列主顺序,您可能需要在将数据发送到函数之前对其进行转置。下面是一个例子,你需要填入空格:
/*=========================================================
* Built on:
* matrixDivide.c - Example for illustrating how to use
* LAPACK within a C MEX-file.
*
* This is a MEX-file for MATLAB.
* Copyright 2009 The MathWorks, Inc.
*=======================================================*/
/* $Revision: 1.1.6.2 $ $Date: 2009/05/18 19:50:18 $ */
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double * pX, * pNumHiddenUnits, * pNumIter, * pkCD; /* pointers to inputs */
double * pOutput; /* output arugments */
mwSignedIndex m,n; /* matrix dimensions */
int i, j;
/* Check for proper number of arguments. */
if ( nrhs != 4)
{
mexErrMsgIdAndTxt("MATLAB:RBM:rhs",
"This function requires 4 inputs.");
}
pX = mxGetPr(prhs[0]); /* pointer to first input, X matrix */
pNumHiddenUnits = mxGetPr(prhs[1]); /* pointer to second input, scalar hidden units */
pNumIter = mxGetPr(prhs[2]); /* pointer to third input, scalar number of iterations */
pkCD = mxGetPr(prhs[3]); /* pointer to third input, scalar kCD */
/* dimensions of input matrix */
m = (mwSignedIndex)mxGetM(prhs[0]);
n = (mwSignedIndex)mxGetN(prhs[0]);
/* Validate input arguments */
if (m < 1 && n < 1)
{
mexErrMsgIdAndTxt("MATLAB:RBM:notamatrix",
"X must be a matrix.");
}
plhs[0] = mxCreateDoubleMatrix(m, n, mxREAL);
pOutput = mxGetPr(plhs[0]);
for (i = 0; i < n; ++i)
{
for (j = 0; j < m; ++j)
{
int index = j * n + i;
pOutput[index] = pX[i * m + j];
}
}
}
/* */
相关文章:
- 如何从用户那里获取输入并将其存储在向量中?
- 代码在执行之前终止,并且没有从用户那里获取输入
- 从用户获取输入/输出
- TFLite 隔离错误,通过获取C++输入和输出
- 如何使 std::istream 从键盘获取输入(如 std::cin)?
- 使用 qt 或 c++ 从桌面获取输入和输出
- 您如何在C++中不断获取输入?
- 从源代码本身C++获取输入
- 函数,从问题中获取输入,并在主函数中使用该输入
- 当我们声明 cin 为 int 并从 cin 中获取输入并在 cout 中打印 cin 时会发生什么?
- 如何在 std::map 中从 std::vector of std::p air 中获取输入?
- 有没有办法将 getline() 与外部文本文件一起使用,然后重新获得 cin 的控制权以从控制台获取输入?
- 从用户获取输入
- 我需要编写一个代码,从用户那里获取输入(句子)并停止在@处
- C 程序在获取输入[控制台应用程序]时跳过行
- 我在C++从函数中的用户获取输入、添加到数组和打印该数组时遇到问题
- 从用户那里获取输入,直到他输入一个数字
- 如何使用数组获取输入,并将其存储在record.h类的主类中的变量中
- getline 语句未获取输入
- 用户定义的功能用于竞争编程中,以获取输入