libsvmwrite.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include "mex.h"
00005 
00006 #ifdef MX_API_VER
00007 #if MX_API_VER < 0x07030000
00008 typedef int mwIndex;
00009 #endif
00010 #endif
00011 
00012 void exit_with_help()
00013 {
00014         mexPrintf(
00015         "Usage: libsvmwrite('filename', label_vector, instance_matrix);\n"
00016         );
00017 }
00018 
00019 static void fake_answer(int nlhs, mxArray *plhs[])
00020 {
00021         int i;
00022         for(i=0;i<nlhs;i++)
00023                 plhs[i] = mxCreateDoubleMatrix(0, 0, mxREAL);
00024 }
00025 
00026 void libsvmwrite(const char *filename, const mxArray *label_vec, const mxArray *instance_mat)
00027 {
00028         FILE *fp = fopen(filename,"w");
00029         mwIndex *ir, *jc, k, low, high;
00030         size_t i, l, label_vector_row_num;
00031         double *samples, *labels;
00032         mxArray *instance_mat_col; // instance sparse matrix in column format
00033 
00034         if(fp ==NULL)
00035         {
00036                 mexPrintf("can't open output file %s\n",filename);                      
00037                 return;
00038         }
00039 
00040         // transpose instance matrix
00041         {
00042                 mxArray *prhs[1], *plhs[1];
00043                 prhs[0] = mxDuplicateArray(instance_mat);
00044                 if(mexCallMATLAB(1, plhs, 1, prhs, "transpose"))
00045                 {
00046                         mexPrintf("Error: cannot transpose instance matrix\n");
00047                         return;
00048                 }
00049                 instance_mat_col = plhs[0];
00050                 mxDestroyArray(prhs[0]);
00051         }
00052 
00053         // the number of instance
00054         l = mxGetN(instance_mat_col);
00055         label_vector_row_num = mxGetM(label_vec);
00056 
00057         if(label_vector_row_num!=l)
00058         {
00059                 mexPrintf("Length of label vector does not match # of instances.\n");
00060                 return;
00061         }
00062 
00063         // each column is one instance
00064         labels = mxGetPr(label_vec);
00065         samples = mxGetPr(instance_mat_col);
00066         ir = mxGetIr(instance_mat_col);
00067         jc = mxGetJc(instance_mat_col);
00068 
00069         for(i=0;i<l;i++)
00070         {
00071                 fprintf(fp,"%g", labels[i]);
00072 
00073                 low = jc[i], high = jc[i+1];
00074                 for(k=low;k<high;k++)
00075                         fprintf(fp," %lu:%g", (size_t)ir[k]+1, samples[k]);             
00076 
00077                 fprintf(fp,"\n");
00078         }
00079 
00080         fclose(fp);
00081         return;
00082 }
00083 
00084 void mexFunction( int nlhs, mxArray *plhs[],
00085                 int nrhs, const mxArray *prhs[] )
00086 {
00087         if(nlhs > 0)
00088         {
00089                 exit_with_help();
00090                 fake_answer(nlhs, plhs);
00091                 return;
00092         }
00093         
00094         // Transform the input Matrix to libsvm format
00095         if(nrhs == 3)
00096         {
00097                 char filename[256];
00098                 if(!mxIsDouble(prhs[1]) || !mxIsDouble(prhs[2]))
00099                 {
00100                         mexPrintf("Error: label vector and instance matrix must be double\n");                  
00101                         return;
00102                 }
00103                 
00104                 mxGetString(prhs[0], filename, mxGetN(prhs[0])+1);              
00105 
00106                 if(mxIsSparse(prhs[2]))
00107                         libsvmwrite(filename, prhs[1], prhs[2]);
00108                 else
00109                 {
00110                         mexPrintf("Instance_matrix must be sparse\n");                  
00111                         return;
00112                 }
00113         }
00114         else
00115         {
00116                 exit_with_help();               
00117                 return;
00118         }
00119 }


target_obejct_detector
Author(s): CIR-KIT
autogenerated on Thu Jun 6 2019 20:19:57