[12011] | 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 |
|
---|
[12365] | 17 | /*FUNCTION MatlabNArrayToNArray(double** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){{{*/
|
---|
[12011] | 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: */
|
---|
[13378] | 30 | mwIndex *ir = NULL;
|
---|
| 31 | mwIndex *jc = NULL;
|
---|
| 32 | double *pr = NULL;
|
---|
| 33 | int count;
|
---|
[12011] | 34 |
|
---|
| 35 | /*get Matlab matrix information: */
|
---|
| 36 | numel=mxGetNumberOfElements(mxmatrix);
|
---|
| 37 | ndims=mxGetNumberOfDimensions(mxmatrix);
|
---|
| 38 | ipt =mxGetDimensions(mxmatrix);
|
---|
[12434] | 39 | size =xNew<int>(ndims);
|
---|
| 40 | for (i=0;i<ndims;i++) size[i]=(int)ipt[i];
|
---|
[12011] | 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: */
|
---|
[13378] | 46 | rows = mxGetM(mxmatrix);
|
---|
| 47 | cols = mxGetN(mxmatrix);
|
---|
[12011] | 48 |
|
---|
[12446] | 49 | matrix=xNewZeroInit<double>(rows*cols);
|
---|
[12011] | 50 |
|
---|
| 51 | /*Now, get ir,jc and pr: */
|
---|
[13378] | 52 | ir = mxGetIr(mxmatrix);
|
---|
| 53 | jc = mxGetJc(mxmatrix);
|
---|
| 54 | pr = mxGetPr(mxmatrix);
|
---|
[12011] | 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: */
|
---|
[12446] | 72 | matrix=xNewZeroInit<double>(numel);
|
---|
[12011] | 73 |
|
---|
[12434] | 74 | dims=xNew<int>(ndims);
|
---|
[12011] | 75 | for(i=0;i<numel;i++){
|
---|
| 76 | ColumnWiseDimsFromIndex(dims,i,size,ndims);
|
---|
[13378] | 77 | j = IndexFromRowWiseDims(dims,size,ndims);
|
---|
| 78 | matrix[j]=(double)mxmatrix_ptr[i];
|
---|
[12011] | 79 | }
|
---|
[12434] | 80 | xDelete<int>(dims);
|
---|
[12011] | 81 | }
|
---|
| 82 |
|
---|
| 83 | /*Assign output pointer: */
|
---|
[13378] | 84 | *pmatrix = matrix;
|
---|
| 85 | *pmatrix_numel = numel;
|
---|
| 86 | *pmatrix_ndims = ndims;
|
---|
| 87 | *pmatrix_size = size;
|
---|
[12011] | 88 |
|
---|
| 89 | return 1;
|
---|
| 90 | }
|
---|
| 91 | /*}}}*/
|
---|
[12365] | 92 | /*FUNCTION MatlabNArrayToNArray(bool** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){{{*/
|
---|
[12011] | 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: */
|
---|
[13378] | 105 | mwIndex *ir = NULL;
|
---|
| 106 | mwIndex *jc = NULL;
|
---|
| 107 | bool *pm = NULL;
|
---|
| 108 | int count;
|
---|
[12011] | 109 |
|
---|
| 110 | /*get Matlab matrix information: */
|
---|
[13378] | 111 | numel = mxGetNumberOfElements(mxmatrix);
|
---|
| 112 | ndims = mxGetNumberOfDimensions(mxmatrix);
|
---|
| 113 | ipt = mxGetDimensions(mxmatrix);
|
---|
| 114 | size = xNew<int>(ndims);
|
---|
[12434] | 115 | for (i=0;i<ndims;i++) size[i]=(int)ipt[i];
|
---|
[12011] | 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);
|
---|
[12446] | 123 | matrix=xNewZeroInit<bool>(rows*cols);
|
---|
[12011] | 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++){
|
---|
[13378] | 134 | matrix[rows*ir[count]+i]=pm[count];
|
---|
[12011] | 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: */
|
---|
[12434] | 145 | matrix=xNew<bool>(numel);
|
---|
| 146 | dims=xNew<int>(ndims);
|
---|
[12011] | 147 | for(i=0;i<numel;i++){
|
---|
| 148 | ColumnWiseDimsFromIndex(dims,i,size,ndims);
|
---|
| 149 | j=IndexFromRowWiseDims(dims,size,ndims);
|
---|
[13378] | 150 | matrix[j]=(bool)mxmatrix_ptr[i];
|
---|
[12011] | 151 | }
|
---|
[12434] | 152 | xDelete<int>(dims);
|
---|
[12011] | 153 | }
|
---|
| 154 |
|
---|
| 155 | /*Assign output pointer: */
|
---|
[13378] | 156 | *pmatrix = matrix;
|
---|
| 157 | *pmatrix_numel = numel;
|
---|
| 158 | *pmatrix_ndims = ndims;
|
---|
| 159 | *pmatrix_size = size;
|
---|
[12011] | 160 |
|
---|
| 161 | return 1;
|
---|
| 162 | }
|
---|
| 163 | /*}}}*/
|
---|
[12365] | 164 | /*FUNCTION MatlabNArrayToNArray(char** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){{{*/
|
---|
[12011] | 165 | int MatlabNArrayToNArray(char** pmatrix,int* pmatrix_numel,int* pmatrix_ndims,int** pmatrix_size,const mxArray* mxmatrix){
|
---|
| 166 |
|
---|
[12595] | 167 | int i,j,rows,cols;
|
---|
| 168 | int numel,ndims;
|
---|
| 169 | int *size , *dims;
|
---|
| 170 | mxChar *mxmatrix_ptr = NULL;
|
---|
| 171 | const mwSize *ipt = NULL;
|
---|
[12011] | 172 |
|
---|
| 173 | /*output: */
|
---|
| 174 | char* matrix=NULL;
|
---|
| 175 |
|
---|
| 176 | /*matlab indices: */
|
---|
[12595] | 177 | mwIndex *ir = NULL;
|
---|
| 178 | mwIndex *jc = NULL;
|
---|
| 179 | char *pm = NULL;
|
---|
| 180 | int count;
|
---|
[12011] | 181 |
|
---|
| 182 | /*get Matlab matrix information: */
|
---|
[13378] | 183 | numel = mxGetNumberOfElements(mxmatrix);
|
---|
| 184 | ndims = mxGetNumberOfDimensions(mxmatrix);
|
---|
| 185 | ipt = mxGetDimensions(mxmatrix);
|
---|
| 186 | size = xNew<int>(ndims);
|
---|
[12434] | 187 | for (i=0;i<ndims;i++) size[i]=(int)ipt[i];
|
---|
[12011] | 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: */
|
---|
[13378] | 193 | rows = mxGetM(mxmatrix);
|
---|
| 194 | cols = mxGetN(mxmatrix);
|
---|
[12434] | 195 | matrix=xNew<char>(rows*cols);
|
---|
[12011] | 196 |
|
---|
| 197 | /*Now, get ir,jc and pm: */
|
---|
[13378] | 198 | ir = mxGetIr(mxmatrix);
|
---|
| 199 | jc = mxGetJc(mxmatrix);
|
---|
| 200 | pm = (char*)mxGetData(mxmatrix);
|
---|
[12011] | 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++){
|
---|
[13378] | 206 | matrix[rows*ir[count]+i]=(char)pm[count];
|
---|
[12011] | 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: */
|
---|
[12434] | 216 | matrix=xNew<char>(numel+1);
|
---|
[12595] | 217 | matrix[numel]='\0';
|
---|
[12011] | 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: */
|
---|
[13378] | 245 | *pmatrix = matrix;
|
---|
| 246 | *pmatrix_numel = numel;
|
---|
| 247 | *pmatrix_ndims = ndims;
|
---|
| 248 | *pmatrix_size = size;
|
---|
[12011] | 249 |
|
---|
| 250 | return 1;
|
---|
| 251 | }
|
---|
| 252 | /*}}}*/
|
---|