2015-10-20 45 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