| [11695] | 1 | /* \file MatlabMatrixToDoubleMatrix.cpp
|
|---|
| 2 | * \brief: convert a sparse or dense matlab matrix to a double* pointer
|
|---|
| 3 | */
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 | #ifdef HAVE_CONFIG_H
|
|---|
| 7 | #include <config.h>
|
|---|
| 8 | #else
|
|---|
| 9 | #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
|
|---|
| 10 | #endif
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| [11861] | 13 | #if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
|
|---|
| [11695] | 14 |
|
|---|
| 15 | /*Matlab includes: */
|
|---|
| 16 | #include "mex.h"
|
|---|
| 17 |
|
|---|
| 18 | #include "../../shared/shared.h"
|
|---|
| 19 |
|
|---|
| 20 | int MatlabMatrixToDoubleMatrix(double** pmatrix,int* pmatrix_rows,int* pmatrix_cols,const mxArray* mxmatrix){
|
|---|
| 21 |
|
|---|
| 22 | int i,j,count,rows,cols;
|
|---|
| 23 | double *pmxdoublematrix = NULL;
|
|---|
| 24 | float *pmxsinglematrix = NULL;
|
|---|
| 25 |
|
|---|
| 26 | /*output: */
|
|---|
| 27 | double* matrix=NULL;
|
|---|
| 28 |
|
|---|
| 29 | /*matlab indices: */
|
|---|
| 30 | mwIndex* ir=NULL;
|
|---|
| 31 | mwIndex* jc=NULL;
|
|---|
| 32 |
|
|---|
| 33 | /*Ok, first check if we are dealing with a sparse or full matrix: */
|
|---|
| 34 | if (mxIsSparse(mxmatrix)){
|
|---|
| 35 |
|
|---|
| 36 | /*Dealing with sparse matrix: recover size first: */
|
|---|
| 37 | pmxdoublematrix=(double*)mxGetPr(mxmatrix);
|
|---|
| 38 | rows=mxGetM(mxmatrix);
|
|---|
| 39 | cols=mxGetN(mxmatrix);
|
|---|
| [11761] | 40 |
|
|---|
| 41 | if(rows*cols){
|
|---|
| 42 | matrix=(double*)xcalloc(rows*cols,sizeof(double));
|
|---|
| [11695] | 43 |
|
|---|
| [11761] | 44 | /*Now, get ir,jc and pr: */
|
|---|
| 45 | ir=mxGetIr(mxmatrix);
|
|---|
| 46 | jc=mxGetJc(mxmatrix);
|
|---|
| [11695] | 47 |
|
|---|
| [11761] | 48 | /*Now, start inserting data into double* matrix: */
|
|---|
| 49 | count=0;
|
|---|
| 50 | for(i=0;i<cols;i++){
|
|---|
| 51 | for(j=0;j<(jc[i+1]-jc[i]);j++){
|
|---|
| 52 | matrix[rows*ir[count]+i]=pmxdoublematrix[count];
|
|---|
| 53 | count++;
|
|---|
| 54 | }
|
|---|
| [11695] | 55 | }
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | }
|
|---|
| 59 | else if(mxIsClass(mxmatrix,"double")){
|
|---|
| 60 | /*Dealing with dense matrix: recover pointer and size: */
|
|---|
| 61 | pmxdoublematrix=(double*)mxGetPr(mxmatrix);
|
|---|
| 62 | rows=mxGetM(mxmatrix);
|
|---|
| 63 | cols=mxGetN(mxmatrix);
|
|---|
| 64 |
|
|---|
| 65 | /*Create serial matrix: */
|
|---|
| [11761] | 66 | if(rows*cols){
|
|---|
| 67 | matrix=(double*)xcalloc(rows*cols,sizeof(double));
|
|---|
| [11695] | 68 |
|
|---|
| [11761] | 69 | for(i=0;i<rows;i++){
|
|---|
| 70 | for(j=0;j<cols;j++){
|
|---|
| 71 | matrix[cols*i+j]=(double)pmxdoublematrix[rows*j+i];
|
|---|
| 72 | }
|
|---|
| [11695] | 73 | }
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 | else if(mxIsClass(mxmatrix,"single")){
|
|---|
| 77 | /*Dealing with dense matrix: recover pointer and size: */
|
|---|
| 78 | pmxsinglematrix=(float*)mxGetPr(mxmatrix);
|
|---|
| 79 | rows=mxGetM(mxmatrix);
|
|---|
| 80 | cols=mxGetN(mxmatrix);
|
|---|
| 81 |
|
|---|
| 82 | /*Create serial matrix: */
|
|---|
| [11761] | 83 | if(rows*cols){
|
|---|
| 84 | matrix=(double*)xcalloc(rows*cols,sizeof(double));
|
|---|
| [11695] | 85 |
|
|---|
| [11761] | 86 | for(i=0;i<rows;i++){
|
|---|
| 87 | for(j=0;j<cols;j++){
|
|---|
| 88 | matrix[cols*i+j]=(double)pmxsinglematrix[rows*j+i];
|
|---|
| 89 | }
|
|---|
| [11695] | 90 | }
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 | else{
|
|---|
| 94 | _error_("Matlab matrix type Not implemented yet");
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | /*Assign output pointer: */
|
|---|
| 98 | *pmatrix=matrix;
|
|---|
| 99 | *pmatrix_rows=rows;
|
|---|
| 100 | *pmatrix_cols=cols;
|
|---|
| 101 |
|
|---|
| 102 | return 1;
|
|---|
| 103 | }
|
|---|
| 104 | #endif
|
|---|