HelperDataConversionMex.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #ifndef HELPERDATACONVERSIONMEX_H
  2. #define HELPERDATACONVERSIONMEX_H
  3. // STL includes
  4. #include <math.h>
  5. #include <matrix.h>
  6. #include <mex.h>
  7. // NICE-core includes
  8. #include <core/vector/MatrixT.h>
  9. #include <core/vector/VectorT.h>
  10. // Interface for conversion between Matlab and C objects
  11. #include "gp-hik-core/matlab/ConverterMatlabToNICE.h"
  12. #include "gp-hik-core/matlab/ConverterNICEToMatlab.h"
  13. #include "vislearning/cbaselib/Example.h"
  14. namespace NICE {
  15. namespace MatlabConversion {
  16. /**
  17. * @brief Create an Examples class from a given full matrix of features and a matrix/vector of labels.
  18. *
  19. * The Examples object consists of individual Example objects containing the label and a pointer to the provided raw feature data.
  20. * Note: No feature data is copied - an Example only contains a pointer to the raw double data.
  21. * An NICE::Vector is created as an wrapper around this raw double pointer using it, but not copying it.
  22. * You need to take care to delete these wrapper vectors once you're finished working with the Examples object, otherwise you generate a memory leak.
  23. *
  24. * @param p_pArrTrainData double MATLAB matrix containing the features (dimension M) of N samples ( M x N matrix )
  25. * @param p_pArrTrainLabels double MATLAB matrix containing the labels for N samples (1xN)
  26. *
  27. * @param p_ExamplesTrain created Examples class (vector of N Example object with each Example containing a valid vec-ptr to the feature data [uncopied] )
  28. *
  29. * @return true for successful Examples creation
  30. * @author Johannes Ruehle
  31. */
  32. bool convertDoubleRawPointersToExamples( const mxArray *p_pArrTrainData, const mxArray *p_pArrTrainLabels, OBJREC::Examples &p_ExamplesTrain )
  33. {
  34. int iNumFeatureDimension = mxGetM( p_pArrTrainData ); // feature dimensions
  35. NICE::Vector yValuesTrain = convertDoubleVectorToNice( p_pArrTrainLabels );
  36. NICE::Matrix matDataTrain = convertDoubleMatrixToNice( p_pArrTrainData );
  37. assert( yValuesTrain.size() == matDataTrain.cols() );
  38. assert( iNumFeatureDimension == matDataTrain.rows() );
  39. p_ExamplesTrain.reserve( matDataTrain.cols() );
  40. const double *pDataPtr = matDataTrain.getDataPointer();
  41. for (int i = 0; i < (int)matDataTrain.cols(); i++, pDataPtr+= iNumFeatureDimension )
  42. {
  43. NICE::Vector *t_pVecTrainData = new NICE::Vector( pDataPtr , (size_t)iNumFeatureDimension);
  44. OBJREC::Example t_Example;
  45. t_Example.vec = t_pVecTrainData;
  46. p_ExamplesTrain.push_back( std::pair<int, OBJREC::Example>( (int)yValuesTrain[i], t_Example ) );
  47. }
  48. return true;
  49. }
  50. }
  51. }
  52. #endif //HELPERDATACONVERSIONMEX_H