1 | /* \file MatlabNArrayToNArray.cpp
|
---|
2 | * \brief: convert a sparse or dense matlab n-dimensional array to cpp n-dimensional array
|
---|
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 | #include "../../shared/shared.h"
|
---|
12 | #include "../../include/include.h"
|
---|
13 |
|
---|
14 | #include <mex.h>
|
---|
15 |
|
---|
16 | /*FUNCTION MatlabNArrayToNArray(double** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){{{*/
|
---|
17 | int MatlabNArrayToNArray(double** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){
|
---|
18 |
|
---|
19 | int i,j,rows,cols;
|
---|
20 | int numel,ndims;
|
---|
21 | int *size,*dims;
|
---|
22 | double* mxmatrix_ptr=NULL;
|
---|
23 | const mwSize* ipt=NULL;
|
---|
24 |
|
---|
25 | /*output: */
|
---|
26 | double* matrix=NULL;
|
---|
27 |
|
---|
28 | /*matlab indices: */
|
---|
29 | mwIndex *ir = NULL;
|
---|
30 | mwIndex *jc = NULL;
|
---|
31 | double *pr = NULL;
|
---|
32 | int count;
|
---|
33 |
|
---|
34 | /*get Matlab matrix information: */
|
---|
35 | numel=mxGetNumberOfElements(mxmatrix);
|
---|
36 | ndims=mxGetNumberOfDimensions(mxmatrix);
|
---|
37 | ipt =mxGetDimensions(mxmatrix);
|
---|
38 | size =xNew<int>(ndims);
|
---|
39 | for (i=0;i<ndims;i++) size[i]=(int)ipt[i];
|
---|
40 |
|
---|
41 | /*Ok, first check if we are dealing with a sparse or full matrix: */
|
---|
42 | if (mxIsSparse(mxmatrix)){
|
---|
43 |
|
---|
44 | /*Dealing with sparse matrix: recover size first: */
|
---|
45 | rows = mxGetM(mxmatrix);
|
---|
46 | cols = mxGetN(mxmatrix);
|
---|
47 |
|
---|
48 | matrix=xNewZeroInit<double>(rows*cols);
|
---|
49 |
|
---|
50 | /*Now, get ir,jc and pr: */
|
---|
51 | ir = mxGetIr(mxmatrix);
|
---|
52 | jc = mxGetJc(mxmatrix);
|
---|
53 | pr = mxGetPr(mxmatrix);
|
---|
54 |
|
---|
55 | /*Now, start inserting data into double* matrix: */
|
---|
56 | count=0;
|
---|
57 | for(i=0;i<cols;i++){
|
---|
58 | for(j=0;j<(jc[i+1]-jc[i]);j++){
|
---|
59 | *(matrix+rows*ir[count]+i)=pr[count];
|
---|
60 | count++;
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | }
|
---|
65 | else{
|
---|
66 |
|
---|
67 | /*Dealing with dense matrix: recover pointer and size: */
|
---|
68 | mxmatrix_ptr=(double*)mxGetPr(mxmatrix);
|
---|
69 |
|
---|
70 | /*Create serial matrix: */
|
---|
71 | matrix=xNewZeroInit<double>(numel);
|
---|
72 |
|
---|
73 | dims=xNew<int>(ndims);
|
---|
74 | for(i=0;i<numel;i++){
|
---|
75 | ColumnWiseDimsFromIndex(dims,i,size,ndims);
|
---|
76 | j = IndexFromRowWiseDims(dims,size,ndims);
|
---|
77 | matrix[j]=(double)mxmatrix_ptr[i];
|
---|
78 | }
|
---|
79 | xDelete<int>(dims);
|
---|
80 | }
|
---|
81 |
|
---|
82 | /*Assign output pointer: */
|
---|
83 | *pmatrix = matrix;
|
---|
84 | *pmatrix_numel = numel;
|
---|
85 | *pmatrix_ndims = ndims;
|
---|
86 | *pmatrix_size = size;
|
---|
87 |
|
---|
88 | return 1;
|
---|
89 | }
|
---|
90 | /*}}}*/
|
---|
91 | /*FUNCTION MatlabNArrayToNArray(bool** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){{{*/
|
---|
92 | int MatlabNArrayToNArray(bool** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){
|
---|
93 |
|
---|
94 | int i,j,rows,cols;
|
---|
95 | int numel,ndims;
|
---|
96 | int *size,*dims;
|
---|
97 | bool* mxmatrix_ptr=NULL;
|
---|
98 | const mwSize* ipt=NULL;
|
---|
99 |
|
---|
100 | /*output: */
|
---|
101 | bool* matrix=NULL;
|
---|
102 |
|
---|
103 | /*matlab indices: */
|
---|
104 | mwIndex *ir = NULL;
|
---|
105 | mwIndex *jc = NULL;
|
---|
106 | bool *pm = NULL;
|
---|
107 | int count;
|
---|
108 |
|
---|
109 | /*get Matlab matrix information: */
|
---|
110 | numel = mxGetNumberOfElements(mxmatrix);
|
---|
111 | ndims = mxGetNumberOfDimensions(mxmatrix);
|
---|
112 | ipt = mxGetDimensions(mxmatrix);
|
---|
113 | size = xNew<int>(ndims);
|
---|
114 | for (i=0;i<ndims;i++) size[i]=(int)ipt[i];
|
---|
115 |
|
---|
116 | /*Ok, first check if we are dealing with a sparse or full matrix: */
|
---|
117 | if (mxIsSparse(mxmatrix)){
|
---|
118 |
|
---|
119 | /*Dealing with sparse matrix: recover size first: */
|
---|
120 | rows=mxGetM(mxmatrix);
|
---|
121 | cols=mxGetN(mxmatrix);
|
---|
122 | matrix=xNewZeroInit<bool>(rows*cols);
|
---|
123 |
|
---|
124 | /*Now, get ir,jc and pm: */
|
---|
125 | ir=mxGetIr(mxmatrix);
|
---|
126 | jc=mxGetJc(mxmatrix);
|
---|
127 | pm=(bool*)mxGetData(mxmatrix);
|
---|
128 |
|
---|
129 | /*Now, start inserting data into bool* matrix: */
|
---|
130 | count=0;
|
---|
131 | for(i=0;i<cols;i++){
|
---|
132 | for(j=0;j<(jc[i+1]-jc[i]);j++){
|
---|
133 | matrix[rows*ir[count]+i]=pm[count];
|
---|
134 | count++;
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|
138 | else{
|
---|
139 |
|
---|
140 | /*Dealing with dense matrix: recover pointer and size: */
|
---|
141 | mxmatrix_ptr=(bool*)mxGetData(mxmatrix);
|
---|
142 |
|
---|
143 | /*Create serial matrix: */
|
---|
144 | matrix=xNew<bool>(numel);
|
---|
145 | dims=xNew<int>(ndims);
|
---|
146 | for(i=0;i<numel;i++){
|
---|
147 | ColumnWiseDimsFromIndex(dims,i,size,ndims);
|
---|
148 | j=IndexFromRowWiseDims(dims,size,ndims);
|
---|
149 | matrix[j]=(bool)mxmatrix_ptr[i];
|
---|
150 | }
|
---|
151 | xDelete<int>(dims);
|
---|
152 | }
|
---|
153 |
|
---|
154 | /*Assign output pointer: */
|
---|
155 | *pmatrix = matrix;
|
---|
156 | *pmatrix_numel = numel;
|
---|
157 | *pmatrix_ndims = ndims;
|
---|
158 | *pmatrix_size = size;
|
---|
159 |
|
---|
160 | return 1;
|
---|
161 | }
|
---|
162 | /*}}}*/
|
---|
163 | /*FUNCTION MatlabNArrayToNArray(char** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){{{*/
|
---|
164 | int MatlabNArrayToNArray(char** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){
|
---|
165 |
|
---|
166 | int i,j,rows,cols;
|
---|
167 | int numel,ndims;
|
---|
168 | int *size , *dims;
|
---|
169 | mxChar *mxmatrix_ptr = NULL;
|
---|
170 | const mwSize *ipt = NULL;
|
---|
171 |
|
---|
172 | /*output: */
|
---|
173 | char* matrix=NULL;
|
---|
174 |
|
---|
175 | /*matlab indices: */
|
---|
176 | mwIndex *ir = NULL;
|
---|
177 | mwIndex *jc = NULL;
|
---|
178 | char *pm = NULL;
|
---|
179 | int count;
|
---|
180 |
|
---|
181 | /*get Matlab matrix information: */
|
---|
182 | numel = mxGetNumberOfElements(mxmatrix);
|
---|
183 | ndims = mxGetNumberOfDimensions(mxmatrix);
|
---|
184 | ipt = mxGetDimensions(mxmatrix);
|
---|
185 | size = xNew<int>(ndims);
|
---|
186 | for (i=0;i<ndims;i++) size[i]=(int)ipt[i];
|
---|
187 |
|
---|
188 | /*Ok, first check if we are dealing with a sparse or full matrix: */
|
---|
189 | if (mxIsSparse(mxmatrix)){
|
---|
190 |
|
---|
191 | /*Dealing with sparse matrix: recover size first: */
|
---|
192 | rows = mxGetM(mxmatrix);
|
---|
193 | cols = mxGetN(mxmatrix);
|
---|
194 | matrix=xNew<char>(rows*cols);
|
---|
195 |
|
---|
196 | /*Now, get ir,jc and pm: */
|
---|
197 | ir = mxGetIr(mxmatrix);
|
---|
198 | jc = mxGetJc(mxmatrix);
|
---|
199 | pm = (char*)mxGetData(mxmatrix);
|
---|
200 |
|
---|
201 | /*Now, start inserting data into char* matrix: */
|
---|
202 | count=0;
|
---|
203 | for(i=0;i<cols;i++){
|
---|
204 | for(j=0;j<(jc[i+1]-jc[i]);j++){
|
---|
205 | matrix[rows*ir[count]+i]=(char)pm[count];
|
---|
206 | count++;
|
---|
207 | }
|
---|
208 | }
|
---|
209 | }
|
---|
210 | else{
|
---|
211 | /*Dealing with dense matrix: recover pointer and size: */
|
---|
212 | mxmatrix_ptr=mxGetChars(mxmatrix);
|
---|
213 |
|
---|
214 | /*Create serial matrix: */
|
---|
215 | matrix=xNew<char>(numel+1);
|
---|
216 | matrix[numel]='\0';
|
---|
217 |
|
---|
218 | /*looping code adapted from Matlab example explore.c: */
|
---|
219 | int elements_per_page = size[0] * size[1];
|
---|
220 | /* total_number_of_pages = size[2] x size[3] x ... x size[N-1] */
|
---|
221 | int total_number_of_pages = 1;
|
---|
222 | for (i=2; i<ndims; i++) {
|
---|
223 | total_number_of_pages *= size[i];
|
---|
224 | }
|
---|
225 |
|
---|
226 | i=0;
|
---|
227 | for (int page=0; page < total_number_of_pages; page++) {
|
---|
228 | int row;
|
---|
229 | /* On each page, walk through each row. */
|
---|
230 | for (row=0; row<size[0]; row++) {
|
---|
231 | int column;
|
---|
232 | j = (page * elements_per_page) + row;
|
---|
233 |
|
---|
234 | /* Walk along each column in the current row. */
|
---|
235 | for (column=0; column<size[1]; column++) {
|
---|
236 | *(matrix+i++)=(char)*(mxmatrix_ptr+j);
|
---|
237 | j += size[0];
|
---|
238 | }
|
---|
239 | }
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | /*Assign output pointer: */
|
---|
244 | *pmatrix = matrix;
|
---|
245 | *pmatrix_numel = numel;
|
---|
246 | *pmatrix_ndims = ndims;
|
---|
247 | *pmatrix_size = size;
|
---|
248 |
|
---|
249 | return 1;
|
---|
250 | }
|
---|
251 | /*}}}*/
|
---|