8 #if MX_API_VER < 0x07030000 13 #define NUM_OF_RETURN_FIELD 10 15 #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) 34 mxArray *return_model, **rhs;
40 rhs[out_id] = mxCreateDoubleMatrix(5, 1, mxREAL);
41 ptr = mxGetPr(rhs[out_id]);
50 rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL);
51 ptr = mxGetPr(rhs[out_id]);
56 rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL);
57 ptr = mxGetPr(rhs[out_id]);
63 rhs[out_id] = mxCreateDoubleMatrix(n, 1, mxREAL);
64 ptr = mxGetPr(rhs[out_id]);
65 for(i = 0; i < n; i++)
66 ptr[i] = model->
rho[i];
72 rhs[out_id] = mxCreateDoubleMatrix(model->
nr_class, 1, mxREAL);
73 ptr = mxGetPr(rhs[out_id]);
75 ptr[i] = model->
label[i];
78 rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL);
82 if(model->
probA != NULL)
84 rhs[out_id] = mxCreateDoubleMatrix(n, 1, mxREAL);
85 ptr = mxGetPr(rhs[out_id]);
86 for(i = 0; i < n; i++)
87 ptr[i] = model->
probA[i];
90 rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL);
94 if(model->
probB != NULL)
96 rhs[out_id] = mxCreateDoubleMatrix(n, 1, mxREAL);
97 ptr = mxGetPr(rhs[out_id]);
98 for(i = 0; i < n; i++)
99 ptr[i] = model->
probB[i];
102 rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL);
108 rhs[out_id] = mxCreateDoubleMatrix(model->
nr_class, 1, mxREAL);
109 ptr = mxGetPr(rhs[out_id]);
110 for(i = 0; i < model->
nr_class; i++)
111 ptr[i] = model->
nSV[i];
114 rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL);
118 rhs[out_id] = mxCreateDoubleMatrix(model->
l, model->
nr_class-1, mxREAL);
119 ptr = mxGetPr(rhs[out_id]);
120 for(i = 0; i < model->
nr_class-1; i++)
121 for(j = 0; j < model->
l; j++)
122 ptr[(i*(model->
l))+j] = model->
sv_coef[i][j];
127 int ir_index, nonzero_element;
129 mxArray *pprhs[1], *pplhs[1];
133 nonzero_element = model->
l;
139 for(i = 0; i < model->
l; i++) {
141 while(model->
SV[i][j].
index != -1)
150 rhs[out_id] = mxCreateSparse(num_of_feature, model->
l, nonzero_element, mxREAL);
151 ir = mxGetIr(rhs[out_id]);
152 jc = mxGetJc(rhs[out_id]);
153 ptr = mxGetPr(rhs[out_id]);
154 jc[0] = ir_index = 0;
155 for(i = 0;i < model->
l; i++)
161 ptr[ir_index] = model->
SV[i][0].
value;
168 while (model->
SV[i][x_index].
index != -1)
170 ir[ir_index] = model->
SV[i][x_index].
index - 1;
171 ptr[ir_index] = model->
SV[i][x_index].
value;
172 ir_index++, x_index++;
174 jc[i+1] = jc[i] + x_index;
178 pprhs[0] = rhs[out_id];
179 if(mexCallMATLAB(1, pplhs, 1, pprhs,
"transpose"))
180 return "cannot transpose SV matrix";
181 rhs[out_id] = pplhs[0];
190 mxSetField(return_model,0,
field_names[i],mxDuplicateArray(rhs[i]));
192 plhs[0] = return_model;
200 int i, j, n, num_of_fields;
207 num_of_fields = mxGetNumberOfFields(matlab_struct);
210 *msg =
"number of return field is not correct";
213 rhs = (mxArray **) mxMalloc(
sizeof(mxArray *)*num_of_fields);
215 for(i=0;i<num_of_fields;i++)
216 rhs[i] = mxGetFieldByNumber(matlab_struct, 0, i);
226 ptr = mxGetPr(rhs[
id]);
234 ptr = mxGetPr(rhs[
id]);
238 ptr = mxGetPr(rhs[
id]);
239 model->
l = (int)ptr[0];
244 model->
rho = (
double*) malloc(n*
sizeof(
double));
245 ptr = mxGetPr(rhs[
id]);
247 model->
rho[i] = ptr[i];
251 if(mxIsEmpty(rhs[
id]) == 0)
254 ptr = mxGetPr(rhs[
id]);
256 model->
label[i] = (
int)ptr[i];
261 if(mxIsEmpty(rhs[
id]) == 0)
263 model->
probA = (
double*) malloc(n*
sizeof(
double));
264 ptr = mxGetPr(rhs[
id]);
266 model->
probA[i] = ptr[i];
271 if(mxIsEmpty(rhs[
id]) == 0)
273 model->
probB = (
double*) malloc(n*
sizeof(
double));
274 ptr = mxGetPr(rhs[
id]);
276 model->
probB[i] = ptr[i];
281 if(mxIsEmpty(rhs[
id]) == 0)
283 model->
nSV = (
int*) malloc(model->
nr_class*
sizeof(
int));
284 ptr = mxGetPr(rhs[
id]);
286 model->
nSV[i] = (
int)ptr[i];
291 ptr = mxGetPr(rhs[
id]);
292 model->
sv_coef = (
double**) malloc((model->
nr_class-1)*
sizeof(double));
293 for( i=0 ; i< model->
nr_class -1 ; i++ )
294 model->
sv_coef[i] = (
double*) malloc((model->
l)*
sizeof(
double));
295 for(i = 0; i < model->
nr_class - 1; i++)
296 for(j = 0; j < model->
l; j++)
297 model->
sv_coef[i][j] = ptr[i*(model->
l)+j];
302 int sr, sc, elements;
305 mxArray *pprhs[1], *pplhs[1];
309 if(mexCallMATLAB(1, pplhs, 1, pprhs,
"transpose"))
312 *msg =
"cannot transpose SV matrix";
317 sr = (int)mxGetN(rhs[
id]);
318 sc = (int)mxGetM(rhs[
id]);
320 ptr = mxGetPr(rhs[
id]);
321 ir = mxGetIr(rhs[
id]);
322 jc = mxGetJc(rhs[
id]);
324 num_samples = (int)mxGetNzmax(rhs[
id]);
326 elements = num_samples + sr;
334 int low = (int)jc[i], high = (
int)jc[i+1];
336 model->
SV[i] = &x_space[low+i];
337 for(j=low;j<high;j++)
339 model->
SV[i][x_index].
index = (int)ir[j] + 1;
340 model->
SV[i][x_index].
value = ptr[j];
343 model->
SV[i][x_index].
index = -1;
static const char * field_names[]
#define NUM_OF_RETURN_FIELD
struct svm_model * matlab_matrix_to_model(const mxArray *matlab_struct, const char **msg)
void svm_free_and_destroy_model(svm_model **model_ptr_ptr)
struct svm_parameter param
const char * model_to_matlab_structure(mxArray *plhs[], int num_of_feature, struct svm_model *model)
struct svm_node * x_space