You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

libsvmwrite.c 2.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "mex.h"
  5. #ifdef MX_API_VER
  6. #if MX_API_VER < 0x07030000
  7. typedef int mwIndex;
  8. #endif
  9. #endif
  10. void exit_with_help()
  11. {
  12. mexPrintf(
  13. "Usage: libsvmwrite('filename', label_vector, instance_matrix);\n"
  14. );
  15. }
  16. static void fake_answer(int nlhs, mxArray *plhs[])
  17. {
  18. int i;
  19. for(i=0;i<nlhs;i++)
  20. plhs[i] = mxCreateDoubleMatrix(0, 0, mxREAL);
  21. }
  22. void libsvmwrite(const char *filename, const mxArray *label_vec, const mxArray *instance_mat)
  23. {
  24. FILE *fp = fopen(filename,"w");
  25. mwIndex *ir, *jc, k, low, high;
  26. size_t i, l, label_vector_row_num;
  27. double *samples, *labels;
  28. mxArray *instance_mat_col; // instance sparse matrix in column format
  29. if(fp ==NULL)
  30. {
  31. mexPrintf("can't open output file %s\n",filename);
  32. return;
  33. }
  34. // transpose instance matrix
  35. {
  36. mxArray *prhs[1], *plhs[1];
  37. prhs[0] = mxDuplicateArray(instance_mat);
  38. if(mexCallMATLAB(1, plhs, 1, prhs, "transpose"))
  39. {
  40. mexPrintf("Error: cannot transpose instance matrix\n");
  41. return;
  42. }
  43. instance_mat_col = plhs[0];
  44. mxDestroyArray(prhs[0]);
  45. }
  46. // the number of instance
  47. l = mxGetN(instance_mat_col);
  48. label_vector_row_num = mxGetM(label_vec);
  49. if(label_vector_row_num!=l)
  50. {
  51. mexPrintf("Length of label vector does not match # of instances.\n");
  52. return;
  53. }
  54. // each column is one instance
  55. labels = mxGetPr(label_vec);
  56. samples = mxGetPr(instance_mat_col);
  57. ir = mxGetIr(instance_mat_col);
  58. jc = mxGetJc(instance_mat_col);
  59. for(i=0;i<l;i++)
  60. {
  61. fprintf(fp,"%g", labels[i]);
  62. low = jc[i], high = jc[i+1];
  63. for(k=low;k<high;k++)
  64. fprintf(fp," %lu:%g", (size_t)ir[k]+1, samples[k]);
  65. fprintf(fp,"\n");
  66. }
  67. fclose(fp);
  68. return;
  69. }
  70. void mexFunction( int nlhs, mxArray *plhs[],
  71. int nrhs, const mxArray *prhs[] )
  72. {
  73. if(nlhs > 0)
  74. {
  75. exit_with_help();
  76. fake_answer(nlhs, plhs);
  77. return;
  78. }
  79. // Transform the input Matrix to libsvm format
  80. if(nrhs == 3)
  81. {
  82. char filename[256];
  83. if(!mxIsDouble(prhs[1]) || !mxIsDouble(prhs[2]))
  84. {
  85. mexPrintf("Error: label vector and instance matrix must be double\n");
  86. return;
  87. }
  88. mxGetString(prhs[0], filename, mxGetN(prhs[0])+1);
  89. if(mxIsSparse(prhs[2]))
  90. libsvmwrite(filename, prhs[1], prhs[2]);
  91. else
  92. {
  93. mexPrintf("Instance_matrix must be sparse\n");
  94. return;
  95. }
  96. }
  97. else
  98. {
  99. exit_with_help();
  100. return;
  101. }
  102. }

A Python package for graph kernels, graph edit distances and graph pre-image problem.