8 #if MX_API_VER < 0x07030000 13 #define NUM_OF_RETURN_FIELD 11 15 #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) 36 mxArray *return_model, **rhs;
42 rhs[out_id] = mxCreateDoubleMatrix(5, 1, mxREAL);
43 ptr = mxGetPr(rhs[out_id]);
52 rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL);
53 ptr = mxGetPr(rhs[out_id]);
58 rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL);
59 ptr = mxGetPr(rhs[out_id]);
65 rhs[out_id] = mxCreateDoubleMatrix(n, 1, mxREAL);
66 ptr = mxGetPr(rhs[out_id]);
67 for (i = 0; i < n; i++)
68 ptr[i] = model->
rho[i];
74 rhs[out_id] = mxCreateDoubleMatrix(model->
nr_class, 1, mxREAL);
75 ptr = mxGetPr(rhs[out_id]);
76 for (i = 0; i < model->
nr_class; i++)
77 ptr[i] = model->
label[i];
80 rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL);
86 rhs[out_id] = mxCreateDoubleMatrix(model->
l, 1, mxREAL);
87 ptr = mxGetPr(rhs[out_id]);
88 for (i = 0; i < model->
l; i++)
92 rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL);
96 if (model->
probA != NULL)
98 rhs[out_id] = mxCreateDoubleMatrix(n, 1, mxREAL);
99 ptr = mxGetPr(rhs[out_id]);
100 for (i = 0; i < n; i++)
101 ptr[i] = model->
probA[i];
104 rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL);
108 if (model->
probB != NULL)
110 rhs[out_id] = mxCreateDoubleMatrix(n, 1, mxREAL);
111 ptr = mxGetPr(rhs[out_id]);
112 for (i = 0; i < n; i++)
113 ptr[i] = model->
probB[i];
116 rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL);
122 rhs[out_id] = mxCreateDoubleMatrix(model->
nr_class, 1, mxREAL);
123 ptr = mxGetPr(rhs[out_id]);
124 for (i = 0; i < model->
nr_class; i++)
125 ptr[i] = model->
nSV[i];
128 rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL);
132 rhs[out_id] = mxCreateDoubleMatrix(model->
l, model->
nr_class - 1, mxREAL);
133 ptr = mxGetPr(rhs[out_id]);
134 for (i = 0; i < model->
nr_class - 1; i++)
135 for (j = 0; j < model->
l; j++)
136 ptr[(i * (model->
l)) + j] = model->
sv_coef[i][j];
141 int ir_index, nonzero_element;
143 mxArray *pprhs[1], *pplhs[1];
147 nonzero_element = model->
l;
153 for (i = 0; i < model->
l; i++)
156 while (model->
SV[i][j].
index != -1)
165 rhs[out_id] = mxCreateSparse(num_of_feature, model->
l, nonzero_element, mxREAL);
166 ir = mxGetIr(rhs[out_id]);
167 jc = mxGetJc(rhs[out_id]);
168 ptr = mxGetPr(rhs[out_id]);
169 jc[0] = ir_index = 0;
170 for (i = 0; i < model->
l; i++)
176 ptr[ir_index] = model->
SV[i][0].
value;
178 jc[i + 1] = jc[i] + 1;
183 while (model->
SV[i][x_index].
index != -1)
185 ir[ir_index] = model->
SV[i][x_index].
index - 1;
186 ptr[ir_index] = model->
SV[i][x_index].
value;
187 ir_index++, x_index++;
189 jc[i + 1] = jc[i] + x_index;
193 pprhs[0] = rhs[out_id];
194 if (mexCallMATLAB(1, pplhs, 1, pprhs,
"transpose"))
195 return "cannot transpose SV matrix";
196 rhs[out_id] = pplhs[0];
205 mxSetField(return_model, 0,
field_names[i], mxDuplicateArray(rhs[i]));
207 plhs[0] = return_model;
215 int i, j, n, num_of_fields;
222 num_of_fields = mxGetNumberOfFields(matlab_struct);
225 *msg =
"number of return field is not correct";
228 rhs = (mxArray **) mxMalloc(
sizeof(mxArray *)*num_of_fields);
230 for (i = 0; i < num_of_fields; i++)
231 rhs[i] = mxGetFieldByNumber(matlab_struct, 0, i);
242 ptr = mxGetPr(rhs[
id]);
250 ptr = mxGetPr(rhs[
id]);
254 ptr = mxGetPr(rhs[
id]);
255 model->
l = (int)ptr[0];
260 model->
rho = (
double*) malloc(n *
sizeof(
double));
261 ptr = mxGetPr(rhs[
id]);
262 for (i = 0; i < n; i++)
263 model->
rho[i] = ptr[i];
267 if (mxIsEmpty(rhs[
id]) == 0)
270 ptr = mxGetPr(rhs[
id]);
271 for (i = 0; i < model->
nr_class; i++)
272 model->
label[i] = (
int)ptr[i];
277 if (mxIsEmpty(rhs[
id]) == 0)
279 model->
sv_indices = (
int*) malloc(model->
l *
sizeof(
int));
280 ptr = mxGetPr(rhs[
id]);
281 for (i = 0; i < model->
l; i++)
287 if (mxIsEmpty(rhs[
id]) == 0)
289 model->
probA = (
double*) malloc(n *
sizeof(
double));
290 ptr = mxGetPr(rhs[
id]);
291 for (i = 0; i < n; i++)
292 model->
probA[i] = ptr[i];
297 if (mxIsEmpty(rhs[
id]) == 0)
299 model->
probB = (
double*) malloc(n *
sizeof(
double));
300 ptr = mxGetPr(rhs[
id]);
301 for (i = 0; i < n; i++)
302 model->
probB[i] = ptr[i];
307 if (mxIsEmpty(rhs[
id]) == 0)
309 model->
nSV = (
int*) malloc(model->
nr_class *
sizeof(
int));
310 ptr = mxGetPr(rhs[
id]);
311 for (i = 0; i < model->
nr_class; i++)
312 model->
nSV[i] = (
int)ptr[i];
317 ptr = mxGetPr(rhs[
id]);
318 model->
sv_coef = (
double**) malloc((model->
nr_class - 1) *
sizeof(double));
319 for (i = 0 ; i < model->
nr_class - 1 ; i++)
320 model->
sv_coef[i] = (
double*) malloc((model->
l) *
sizeof(
double));
321 for (i = 0; i < model->
nr_class - 1; i++)
322 for (j = 0; j < model->
l; j++)
323 model->
sv_coef[i][j] = ptr[i * (model->
l) + j];
328 int sr, sc, elements;
331 mxArray *pprhs[1], *pplhs[1];
335 if (mexCallMATLAB(1, pplhs, 1, pprhs,
"transpose"))
338 *msg =
"cannot transpose SV matrix";
343 sr = (int)mxGetN(rhs[
id]);
344 sc = (int)mxGetM(rhs[
id]);
346 ptr = mxGetPr(rhs[
id]);
347 ir = mxGetIr(rhs[
id]);
348 jc = mxGetJc(rhs[
id]);
350 num_samples = (int)mxGetNzmax(rhs[
id]);
352 elements = num_samples + sr;
358 for (i = 0; i < sr; i++)
360 int low = (int)jc[i], high = (
int)jc[i + 1];
362 model->
SV[i] = &x_space[low + i];
363 for (j = low; j < high; j++)
365 model->
SV[i][x_index].
index = (int)ir[j] + 1;
366 model->
SV[i][x_index].
value = ptr[j];
369 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