source: issm/branches/trunk-jpl-damage/src/c/matlab/io/MatlabMatrixToDoubleMatrix.cpp@ 12168

Last change on this file since 12168 was 12011, checked in by Eric.Larour, 13 years ago

Preliminary commit of new issm version, where serial code is starting to be stripped away. Will not run before some major debugging is done

File size: 2.2 KB
Line 
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
13/*Matlab includes: */
14#include "mex.h"
15
16#include "../../shared/shared.h"
17
18int MatlabMatrixToDoubleMatrix(double** pmatrix,int* pmatrix_rows,int* pmatrix_cols,const mxArray* mxmatrix){
19
20 int i,j,count,rows,cols;
21 double *pmxdoublematrix = NULL;
22 float *pmxsinglematrix = NULL;
23
24 /*output: */
25 double* matrix=NULL;
26
27 /*matlab indices: */
28 mwIndex* ir=NULL;
29 mwIndex* jc=NULL;
30
31 /*Ok, first check if we are dealing with a sparse or full matrix: */
32 if (mxIsSparse(mxmatrix)){
33
34 /*Dealing with sparse matrix: recover size first: */
35 pmxdoublematrix=(double*)mxGetPr(mxmatrix);
36 rows=mxGetM(mxmatrix);
37 cols=mxGetN(mxmatrix);
38
39 if(rows*cols){
40 matrix=(double*)xcalloc(rows*cols,sizeof(double));
41
42 /*Now, get ir,jc and pr: */
43 ir=mxGetIr(mxmatrix);
44 jc=mxGetJc(mxmatrix);
45
46 /*Now, start inserting data into double* matrix: */
47 count=0;
48 for(i=0;i<cols;i++){
49 for(j=0;j<(jc[i+1]-jc[i]);j++){
50 matrix[rows*ir[count]+i]=pmxdoublematrix[count];
51 count++;
52 }
53 }
54 }
55
56 }
57 else if(mxIsClass(mxmatrix,"double")){
58 /*Dealing with dense matrix: recover pointer and size: */
59 pmxdoublematrix=(double*)mxGetPr(mxmatrix);
60 rows=mxGetM(mxmatrix);
61 cols=mxGetN(mxmatrix);
62
63 /*Create serial matrix: */
64 if(rows*cols){
65 matrix=(double*)xcalloc(rows*cols,sizeof(double));
66
67 for(i=0;i<rows;i++){
68 for(j=0;j<cols;j++){
69 matrix[cols*i+j]=(double)pmxdoublematrix[rows*j+i];
70 }
71 }
72 }
73 }
74 else if(mxIsClass(mxmatrix,"single")){
75 /*Dealing with dense matrix: recover pointer and size: */
76 pmxsinglematrix=(float*)mxGetPr(mxmatrix);
77 rows=mxGetM(mxmatrix);
78 cols=mxGetN(mxmatrix);
79
80 /*Create serial matrix: */
81 if(rows*cols){
82 matrix=(double*)xcalloc(rows*cols,sizeof(double));
83
84 for(i=0;i<rows;i++){
85 for(j=0;j<cols;j++){
86 matrix[cols*i+j]=(double)pmxsinglematrix[rows*j+i];
87 }
88 }
89 }
90 }
91 else{
92 _error_("Matlab matrix type Not implemented yet");
93 }
94
95 /*Assign output pointer: */
96 *pmatrix=matrix;
97 *pmatrix_rows=rows;
98 *pmatrix_cols=cols;
99
100 return 1;
101}
Note: See TracBrowser for help on using the repository browser.