ConverterNICEToMatlab.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "ConverterNICEToMatlab.h"
  2. using namespace NICE;
  3. using namespace NICE::MatlabConversion;
  4. // b_adaptIndexCtoM: if true, dim k will be inserted as k, not as k+1 (which would be the default for C->M)
  5. mxArray* MatlabConversion::convertSparseVectorFromNice( const NICE::SparseVector & niceSvec, const bool & b_adaptIndexCtoM )
  6. {
  7. mxArray * matlabSparseVec = mxCreateSparse( niceSvec.getDim() /*m*/, 1/*n*/, niceSvec.size()/*nzmax*/, mxREAL);
  8. // To make the returned sparse mxArray useful, you must initialize the pr, ir, jc, and (if it exists) pi arrays.
  9. // mxCreateSparse allocates space for:
  10. //
  11. // A pr array of length nzmax.
  12. // A pi array of length nzmax, but only if ComplexFlag is mxCOMPLEX in C (1 in Fortran).
  13. // An ir array of length nzmax.
  14. // A jc array of length n+1.
  15. double* prPtr = mxGetPr(matlabSparseVec);
  16. mwIndex * ir = mxGetIr( matlabSparseVec );
  17. mwIndex * jc = mxGetJc( matlabSparseVec );
  18. jc[1] = niceSvec.size(); jc[0] = 0;
  19. mwSize cnt = 0;
  20. for ( NICE::SparseVector::const_iterator myIt = niceSvec.begin(); myIt != niceSvec.end(); myIt++, cnt++ )
  21. {
  22. // set index
  23. if ( b_adaptIndexCtoM )
  24. ir[cnt] = myIt->first-1;
  25. else
  26. ir[cnt] = myIt->first;
  27. // set value
  28. prPtr[cnt] = myIt->second;
  29. }
  30. return matlabSparseVec;
  31. }
  32. mxArray* MatlabConversion::convertMatrixFromNice( const NICE::Matrix & niceMatrix )
  33. {
  34. mxArray *matlabMatrix = mxCreateDoubleMatrix( niceMatrix.rows(), niceMatrix.cols(), mxREAL );
  35. double* matlabMatrixPtr = mxGetPr( matlabMatrix );
  36. for( int i = 0; i < niceMatrix.rows(); i++ )
  37. {
  38. for( int j = 0; j < niceMatrix.cols(); j++ )
  39. {
  40. matlabMatrixPtr[i + j*niceMatrix.rows()] = niceMatrix(i,j);
  41. }
  42. }
  43. return matlabMatrix;
  44. }
  45. mxArray* MatlabConversion::convertVectorFromNice( const NICE::Vector & niceVector )
  46. {
  47. mxArray *matlabVector = mxCreateDoubleMatrix( niceVector.size(), 1, mxREAL );
  48. double* matlabVectorPtr = mxGetPr( matlabVector );
  49. for( int i = 0; i < niceVector.size(); i++ )
  50. {
  51. matlabVectorPtr[i] = niceVector[i];
  52. }
  53. return matlabVector;
  54. }