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