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