2015-10-20 123 views
0
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方面有以下内容使用C++ mex获取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); 
+1

你可以找到这在[LIBSVM(一个例子http://www.csie.ntu.edu.tw/~cjlin/LIBSVM /)。寻找svm_model_matlab.h和svm_model_matlab.c。 –

回答

0

标量输入参数是相当简单的。矩阵输入有点棘手,因为它们使用古老的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]; 
     } 
    } 
    } 
    /* */ 
+0

对不起已经生病昨天前夕感染流感..会尝试上述和尽快回来..感谢提示 – JNW

+0

刚刚检查它很好,但输入参数,需要给予的应该是int类型,而不是double ... – JNW

+0

RBM.cpp:在函数'void mexFunction(int,mxArray **,int,const mxArray **)'中: RBM.cpp:465:28:错误:无效从'double *'转换为' int'[-fpermissive] RBM.cpp:466:35:错误:从'double *'无效转换为'int'[-fpermissive] RBM.cpp:468:27:error:can convert'double *'到'双' RBM.cpp:525:23:错误:冲突声明'double train_X [6] [6]' RBM.cpp:454:8:错误:'train_X'有一个前面的声明为'double train_X RBM.cpp:548:45:错误:数组下标的无效类型'double [int]' – JNW