long MarkIdsOnBuckets( const mxArray *PLOC, int mini, int maxi, int minj, int maxj, int mink, int maxk, int *IDs, int MARK ){ long p; long max_id= -1; long n_bucket; double *bucket; int i,j,k; int bucket_id[3]; for ( i=mini-1 ; i<maxi ; i++ ) { for ( j=minj-1 ; j<maxj ; j++ ) { for ( k=mink-1 ; k<maxk ; k++ ) { bucket_id[0]=i; bucket_id[1]=j; bucket_id[2]=k; n_bucket = mxGetN( mxGetCell( PLOC , mxCalcSingleSubscript( PLOC,3,bucket_id ))); bucket = mxGetPr( mxGetCell( PLOC , mxCalcSingleSubscript( PLOC,3,bucket_id ))); for ( p=0; p<n_bucket; p++){ if ( IDs[ (int)bucket[p]-1 ] == 0 ){ if( bucket[p]-1 > max_id ) { max_id= (int)bucket[p]-1; } IDs[ (int)bucket[p]-1 ] = MARK; } } } } } return max_id; }
/* Returns an mxArray representing the units of image im. * * Parameters: * Return: the array, or NULL on failure. */ mxArray *units2mx(const Image *const im) { mxArray *mx; double *mxData; int i; // Create an array if ((mx = mxCreateDoubleMatrix(IM_NDIMS, 1, mxREAL)) == NULL) return NULL; // Get the data if ((mxData = mxGetData(mx)) == NULL) goto units2mx_quit; // Copy the data from im for (i = 0; i < IM_NDIMS; i++) { mwIndex idx; const mwIndex subs[] = {i, 0}; const mwIndex nSubs = sizeof(subs) / sizeof(mwIndex); idx = mxCalcSingleSubscript(mx, nSubs, subs); mxData[idx] = SIFT3D_IM_GET_UNITS(im)[i]; } return mx; units2mx_quit: mxDestroyArray(mx); return NULL; }
/* Set the units of an image to the data stored in an mxArray, * * Parameters: * -mx: An array of IM_NDIMS dimensions, type double. * -im: The Image struct to which the units are written. * * Return: SIFT3D_SUCCESS on success, SIFT3D_FAILURE otherwise. */ int mx2units(const mxArray *const mx, Image *const im) { const mwSize *mxDims; double *mxData; mwIndex mxNDims; int i; // Verify inputs mxNDims = (int) mxGetNumberOfDimensions(mx); if (mxNDims != 2 || !isDouble(mx)) return SIFT3D_FAILURE; // Verify the dimensions mxDims = mxGetDimensions(mx); if (mxDims[0] != IM_NDIMS || mxDims[1] != 1) return SIFT3D_FAILURE; // Get the data if ((mxData = mxGetData(mx)) == NULL) return SIFT3D_FAILURE; // Copy the data to im for (i = 0; i < IM_NDIMS; i++) { mwIndex idx; const mwIndex subs[] = {i, 0}; const mwIndex nSubs = sizeof(subs) / sizeof(mwIndex); idx = mxCalcSingleSubscript(mx, nSubs, subs); SIFT3D_IM_GET_UNITS(im)[i] = mxData[idx]; } return SIFT3D_SUCCESS; }
mwIndex MxArray::subs(mwIndex i, mwIndex j) const { if (i < 0 || i >= rows() || j < 0 || j >= cols()) mexErrMsgIdAndTxt("mexopencv:error", "Subscript out of range"); mwIndex s[] = {i, j}; return mxCalcSingleSubscript(p_, 2, s); }
/* Returns the index in an mxArray of the voxel at the coordinates (x, y, z) * and channel c */ mwIndex mxImGetIdx(const mxArray *const mx, const int x, const int y, const int z, const int c) { const mwIndex subs[] = {x, y, z, c}; const mwSize nSubs = sizeof(subs) / sizeof(mwIndex); assert(nSubs == MX_IM_NDIMS); return mxCalcSingleSubscript(mx, nSubs, subs); }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { int i, j; int N, Nperm; int *d; int subs[2]; double *ptr; gsl_permutation *c; if(nrhs != 1) mexErrMsgTxt("1 input required"); if(nlhs != 1) mexErrMsgTxt("Requires one output."); if(mxGetM(prhs[0]) * mxGetN(prhs[0]) != 1) mexErrMsgTxt("N must be scalar"); N = mxGetScalar(prhs[0]); Nperm = (int) (gsl_sf_fact(N)); c = gsl_permutation_calloc (N); gsl_permutation_init(c); plhs[0] = mxCreateDoubleMatrix(Nperm, N, mxREAL); ptr = mxGetPr(plhs[0]); for(i = 0; i < Nperm; i++) { d = gsl_permutation_data(c); for(j = 0; j < N; j++) { subs[0] = i; subs[1] = j; ptr[mxCalcSingleSubscript(plhs[0], 2, subs)] = (double)d[j] + 1.; } gsl_permutation_next(c); } gsl_permutation_free(c); }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { if ( nrhs != 2 ) { mexErrMsgIdAndTxt("MATLAB:mexTest:rhs","One for !"); } mxArray *helper, *retCell; struct GMPmat *myHelp, *calcMat; helper = VertConcat( prhs[1] , prhs[0] ); myHelp = GMPmat_fromMXArray( helper ); mxDestroyArray(helper); GMPmat_invertSignForFacetEnumeration(myHelp); calcMat = H2V(myHelp); helper = MXArray_fromGMPmat(calcMat); GMPmat_destroy(calcMat); retCell = VertBreakdown(helper); mxDestroyArray(helper); if (nlhs==2){ mwSize nsubs=2, subs[2]; mwIndex index; subs[0] = 0; subs[1] = 0; index = mxCalcSingleSubscript(retCell, nsubs, subs); plhs[0] = mxGetCell(retCell,index); subs[1] = 1; index = mxCalcSingleSubscript(retCell, nsubs, subs); plhs[1] = mxGetCell(retCell,index); } else { plhs[0] = retCell; } mxDestroyArray(retCell); }
void copyArray(prob_t* to, const mxArray* from, size_t size) { double* v = mxGetPr(from); size_t i; mwSize nsubs = mxGetNumberOfDimensions(from); mwIndex* subs = mxCalloc(nsubs,sizeof(mwIndex)); mwIndex index; for (i = 0; i < size; i++) { subs[0] = i; index = mxCalcSingleSubscript(from, nsubs, subs); to[i] = v[index]; } mxFree(subs); }
// calcSingleSubscript // gets linear index from matrix subscripts. Note matrix subscripts // should be zero-based not one-based mwIndex calcSingleSubscript (std::vector<mwSize> index) { // check dimensions are within range checkDimensions (index); // make an array of the appropriate size to hold the indices mwIndex* subs = new mwIndex[_dimensions.size ()]; // copy the index into the subs array for (int i=0; i < index.size (); i++) { *(subs+i) = index[i]; } // get the linear index into the underlying data array mwIndex linindex = mxCalcSingleSubscript(wMxArray, (mwSize)(_dimensions.size ()), subs); // delete the memory allocated for the index delete[] subs; return linindex; }
mxArray* copyArrayToMatlab(prob_t* in, size_t size) { mxArray *out = mxCreateDoubleMatrix(1, size, mxREAL); mwSize nsubs = mxGetNumberOfDimensions(out); mwIndex* subs = mxCalloc(nsubs,sizeof(mwIndex)); double* m = mxGetPr(out); mwIndex index; size_t i; for (i = 0; i < size; i++) { subs[1] = i; index = mxCalcSingleSubscript(out, nsubs, subs); m[index] = in[i]; } mxFree(subs); return out; }
static void copyPrombsMatrix(prob_t** to, const mxArray* from, size_t L) { double* m = mxGetPr(from); size_t i, j; mwSize nsubs = mxGetNumberOfDimensions(from); mwIndex* subs = mxCalloc(nsubs,sizeof(mwIndex)); mwIndex index; for (i = 0; i < L; i++) { for (j = 0; j < L; j++) { subs[0] = i; subs[1] = j; index = mxCalcSingleSubscript(from, nsubs, subs); to[i][j] = m[index]; } } mxFree(subs); }
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]){ int N = 0, Ndet = 0, Nang = 0; mxArray* cell_element_ptr = NULL; // One X-ray data double *cellContent = NULL, *rsum_ptr = NULL, *csum_ptr = NULL, val = 0/*weight*/; int CellIndex = 0, PixelIndex = 0, nsubs = 2, subs[2] = {0,0}, length = 0; // check inputs if(nrhs != 2){ mexErrMsgTxt("Incorrect number of inputs. Function expects 2 inputs.\n"); } if(nlhs != 2){ mexErrMsgTxt("Incorrect number of outputs. Function expects 2 outputs.\n"); } if(!mxIsCell(prhs[0])){ mexErrMsgTxt("SM must be a cell array!\n"); } Ndet = mxGetM(prhs[0]); Nang = mxGetN(prhs[0]); N = *mxGetPr(prhs[1]); // Create Output plhs[0] = mxCreateDoubleMatrix(N,N,mxREAL); plhs[1] = mxCreateDoubleMatrix(Ndet,Nang,mxREAL); rsum_ptr = mxGetPr(plhs[0]); csum_ptr = mxGetPr(plhs[1]); // summation for(int i = 0; i < Ndet; i++){ for(int j = 0; j < Nang; j++){ subs[0] = i; subs[1] = j; CellIndex = mxCalcSingleSubscript(prhs[0], 2, subs); cell_element_ptr = mxGetCell(prhs[0],CellIndex); if (cell_element_ptr){ length = mxGetN(cell_element_ptr); cellContent = mxGetPr(cell_element_ptr); // calculate real projection and squared sum of weights for (int l = 0; l < length; l++){ PixelIndex = cellContent[l * 2] - 1; val = cellContent[l * 2 + 1]; rsum_ptr[PixelIndex] += val; csum_ptr[CellIndex] += val; } } } } }
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[]) { int m, n, p; mwIndex subs[3] = {0, 0, 0}; mwIndex frameInd = 0; BYTE ***image3d; const mwSize *dims = mxGetDimensions(prhs[0]); mwSize width = int(dims[1]); image3d = new BYTE**[width]; BYTE *inpr = (BYTE *)mxGetData(prhs[0]); for(p = 0; p < width; p++) { image3d[p] = New2DPointer<BYTE>(width, width); subs[2] = p; frameInd = mxCalcSingleSubscript(prhs[0], 3, subs); for(n = 0; n < width; n++) { memcpy(image3d[p][n], &inpr[frameInd + n*width], width); } } plhs[0] = mxCreateNumericMatrix(1, 177, mxSINGLE_CLASS, mxREAL); float **hist = New2DPointer<float>(3, 59); for(n = 0; n< 3;n++) memset(hist[n], 0, 236); LBPHist(image3d, width, hist); float *outpr = (float *)mxGetData(plhs[0]); memcpy(&outpr[0], hist[0], 236); memcpy(&outpr[59], hist[1], 236); memcpy(&outpr[108], hist[2], 236); Delete2DPointer(hist, 3); for(p = 0; p < width; p++) { for(n = 0; n < width; n++) free(image3d[p][n]); free(image3d[p]); } free(image3d); }
void copyMatrix(matrix_t* to, const mxArray* from) { const size_t R = to->rows; const size_t C = to->columns; double* m = mxGetPr(from); size_t i, j; mwSize nsubs = mxGetNumberOfDimensions(from); mwIndex* subs = mxCalloc(nsubs,sizeof(mwIndex)); mwIndex index; for (i = 0; i < R; i++) { for (j = 0; j < C; j++) { subs[0] = i; subs[1] = j; index = mxCalcSingleSubscript(from, nsubs, subs); to->content[i][j] = m[index]; } } mxFree(subs); }
mxArray* copyMatrixToMatlab(matrix_t* in) { mxArray *out = mxCreateDoubleMatrix(in->rows, in->columns, mxREAL); mwSize nsubs = mxGetNumberOfDimensions(out); mwIndex* subs = mxCalloc(nsubs,sizeof(mwIndex)); double* m = mxGetPr(out); mwIndex index; size_t i, j; for (i = 0; i < in->rows; i++) { for (j = 0; j < in->columns; j++) { subs[0] = i; subs[1] = j; index = mxCalcSingleSubscript(out, nsubs, subs); m[index] = in->content[i][j]; } } mxFree(subs); return out; }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { mxArray *rhs[2]; mxArray *blk_cell_pr, *A_cell_pr; double *A, *B, *blksize, *AI, *BI; mwIndex *irA, *jcA, *irB, *jcB; int *cumblksize, *blknnz; int mblk, mA, nA, m1, n1, rowidx, colidx, isspA, isspB; int iscellA, iscmpA; mwIndex subs[2]; mwSize nsubs=2; int n, n2, k, nsub, index, numblk, NZmax; double ir2=1/sqrt(2); /* CHECK FOR PROPER NUMBER OF ARGUMENTS */ if (nrhs < 2){ mexErrMsgTxt("mexsmat: requires at least 2 input arguments."); } else if (nlhs>1){ mexErrMsgTxt("mexsmat: requires 1 output argument."); } /* CHECK THE DIMENSIONS */ iscellA = mxIsCell(prhs[1]); if (iscellA) { mA = mxGetM(prhs[1]); nA = mxGetN(prhs[1]); } else { mA = 1; nA = 1; } if (mxGetM(prhs[0]) != mA) { mexErrMsgTxt("mexsmat: blk and Avec not compatible"); } /***** main body *****/ if (nrhs > 3) {rowidx = (int)*mxGetPr(prhs[3]); } else {rowidx = 1;} if (rowidx > mA) { mexErrMsgTxt("mexsmat: rowidx exceeds size(Avec,1)."); } subs[0] = rowidx-1; /* subtract 1 to adjust for Matlab index */ subs[1] = 1; index = mxCalcSingleSubscript(prhs[0],nsubs,subs); blk_cell_pr = mxGetCell(prhs[0],index); if (blk_cell_pr == NULL) { mexErrMsgTxt("mexsmat: blk not properly specified"); } numblk = mxGetN(blk_cell_pr); blksize = mxGetPr(blk_cell_pr); cumblksize = mxCalloc(numblk+1,sizeof(int)); blknnz = mxCalloc(numblk+1,sizeof(int)); cumblksize[0] = 0; blknnz[0] = 0; n = 0; n2 = 0; for (k=0; k<numblk; ++k) { nsub = (int) blksize[k]; n += nsub; n2 += nsub*(nsub+1)/2; cumblksize[k+1] = n; blknnz[k+1] = n2; } /***** assign pointers *****/ if (iscellA) { subs[0] = rowidx-1; subs[1] = 0; index = mxCalcSingleSubscript(prhs[1],nsubs,subs); A_cell_pr = mxGetCell(prhs[1],index); A = mxGetPr(A_cell_pr); m1 = mxGetM(A_cell_pr); n1 = mxGetN(A_cell_pr); isspA = mxIsSparse(A_cell_pr); iscmpA = mxIsComplex(A_cell_pr); if (isspA) { irA = mxGetIr(A_cell_pr); jcA = mxGetJc(A_cell_pr); } if (iscmpA) { AI = mxGetPi(A_cell_pr); } } else { A = mxGetPr(prhs[1]); m1 = mxGetM(prhs[1]); n1 = mxGetN(prhs[1]); isspA = mxIsSparse(prhs[1]); iscmpA = mxIsComplex(prhs[1]); if (isspA) { irA = mxGetIr(prhs[1]); jcA = mxGetJc(prhs[1]); } if (iscmpA) { AI = mxGetPi(prhs[1]); } } if (numblk > 1) { isspB = 1; } else { if (nrhs > 2) {isspB = (int)*mxGetPr(prhs[2]);} else {isspB = isspA;} } if (nrhs > 4) {colidx = (int)*mxGetPr(prhs[4]) -1;} else {colidx = 0;} if (colidx > n1) { mexErrMsgTxt("mexsmat: colidx exceeds size(Avec,2)."); } /***** create return argument *****/ if (isspB) { if (isspA) { NZmax = jcA[colidx+1]-jcA[colidx]; } else { NZmax = blknnz[numblk]; } if (iscmpA) { rhs[0] = mxCreateSparse(n,n,2*NZmax,mxCOMPLEX); } else { rhs[0] = mxCreateSparse(n,n,2*NZmax,mxREAL); } B = mxGetPr(rhs[0]); irB = mxGetIr(rhs[0]); jcB = mxGetJc(rhs[0]); if (iscmpA) { BI = mxGetPi(rhs[0]); } } else { if (iscmpA) { plhs[0] = mxCreateDoubleMatrix(n,n,mxCOMPLEX); } else { plhs[0] = mxCreateDoubleMatrix(n,n,mxREAL); } B = mxGetPr(plhs[0]); if (iscmpA) { BI = mxGetPi(plhs[0]); } } /***** Do the computations in a subroutine *****/ if (iscmpA) { if (numblk == 1) { smat1cmp(n,ir2,A,irA,jcA,isspA,m1,colidx,B,irB,jcB,isspB,AI,BI); } else { smat2cmp(n,numblk,cumblksize,blknnz,ir2,A,irA,jcA,isspA,m1,colidx,B,irB,jcB,isspB,AI,BI); } } else { if (numblk == 1) { smat1(n,ir2,A,irA,jcA,isspA,m1,colidx,B,irB,jcB,isspB); } else { smat2(n,numblk,cumblksize,blknnz,ir2,A,irA,jcA,isspA,m1,colidx,B,irB,jcB,isspB); } } if (isspB) { /*** if isspB, (actual B) = B+B' ****/ mexCallMATLAB(1, &rhs[1], 1, &rhs[0], "ctranspose"); mexCallMATLAB(1, &plhs[0],2, rhs, "+"); mxDestroyArray(*rhs); } mxFree(blknnz); mxFree(cumblksize); return; }
void mexFunction ( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { char *formatstring; char *timestring; mxArray *timestr; double *epoch; int M, N; int nsubs = 2; int subs[2]; int cell_index; int i, j; if( nrhs != 2 ) { antelope_mexUsageMsgTxt ( USAGE ); return; } else if( mxGetClassID( prhs[0] ) != mxDOUBLE_CLASS ) { antelope_mexUsageMsgTxt ( USAGE ); return; } else if( ! mtlb_get_string( prhs[1], &formatstring ) ) { antelope_mexUsageMsgTxt ( USAGE ); return; } epoch = mxGetPr( prhs[0] ); M = mxGetM( prhs[0] ); N = mxGetN( prhs[0] ); if( M == 1 && N == 1 ) { timestring = epoch2str( *epoch, formatstring ); antelope_mex_clear_register( 1 ); plhs[0] = mxCreateString( timestring ); free( timestring ); } else { plhs[0] = mxCreateCellMatrix( M, N ); for( i = 0; i < M; i++ ) { for( j = 0; j < N; j++ ) { timestring = epoch2str( *(epoch + i*N + j), formatstring ); antelope_mex_clear_register( 1 ); timestr = mxCreateString( timestring ); free( timestring ); subs[0] = i; subs[1] = j; cell_index = mxCalcSingleSubscript( plhs[0], nsubs, subs ); mxSetCell( plhs[0], cell_index, timestr ); } } } mxFree( formatstring ); }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { double *tstamp; /* spike timestamps (in 1/10000 sec) */ double *x_pos, *y_pos, *phi_pos; double *on_same_tet; double ***triplets_ts_ptr, ***triplets_x_ptr, ***triplets_y_ptr, ***triplets_phi_ptr; int **triplets_count; int **triplets_last_ts; int *idxs; int cidx[3]; int midx[2]; int n_spikes; /* total number of spike */ int n_triplets_true = 0, n_triplets_chk = 0; /* diagnostics */ int n_combs; /* number of combinations */ int i,j,k, i_last; int omit_double = 1; /* if TRUE, then a spike can't be member of more than one triplet of the same kind, this helps avoiding many doubles that would make the interpretation hard */ double *triplets_freq; double *triples_table; int *triples_lookup; /* direct lookup table */ double time_span; enum {TSTAMP=0, IDXS, X_POS, Y_POS, PHI_POS, NCELLS, TSPAN, ON_SAME_TET } in_arg_order; enum {TRIPLETS_FREQ, TRIPLES_TABLE, TRIPLETS_TS, TRIPLETS_X, TRIPLETS_Y, TRIPLETS_PHI } out_arg_order; if(nrhs != 8 ) mexErrMsgTxt("Eight inputs required"); if(nlhs != 6 ) mexErrMsgTxt("Six outputs required"); /* MEX related stuff, not really related with algorithms */ /* extracting timestamps */ mexPrintf("class of tstamp: %d\n", mxGetClassID(prhs[TSTAMP])); /* DEBUG */ n_spikes = mxGetM(prhs[TSTAMP]); if(mxGetN(prhs[TSTAMP]) != 1) mexErrMsgTxt("Timestamp must be a column vector"); tstamp = mxGetPr(prhs[TSTAMP]); /* extracting cell indices */ if(mxGetM(prhs[IDXS]) != n_spikes) mexErrMsgTxt("indices and timestamps must have same length"); { int i; double *dixs; dixs = mxGetPr(prhs[IDXS]); idxs = (int *) mxCalloc(n_spikes, sizeof(int)); for(i = 0; i < n_spikes; i++) idxs[i] = (int)(dixs[i])-1; } /* extracting location */ mexPrintf("class of x: %d\n", mxGetClassID(prhs[X_POS])); /* DEBUG */ if(mxGetN(prhs[X_POS]) != 1) mexErrMsgTxt("x_pos must be a column vector"); x_pos = mxGetPr(prhs[X_POS]); if(mxGetN(prhs[Y_POS]) != 1) mexErrMsgTxt("y_pos must be a column vector"); y_pos = mxGetPr(prhs[Y_POS]); if(mxGetN(prhs[PHI_POS]) != 1) mexErrMsgTxt("phi_pos must be a column vector"); phi_pos = mxGetPr(prhs[PHI_POS]); /* extracting n_cells */ if(mxGetM(prhs[NCELLS]) * mxGetN(prhs[NCELLS]) != 1) mexErrMsgTxt("n_cells must be a scalar"); n_cells = (int)mxGetScalar(prhs[NCELLS]); /* extracting time_span*/ if(mxGetM(prhs[TSPAN]) * mxGetN(prhs[TSPAN]) != 1) mexErrMsgTxt("time_span must be a scalar"); time_span = (int)mxGetScalar(prhs[TSPAN]); /* extracting on_same_tet */ if(mxGetM(prhs[ON_SAME_TET]) != n_cells || mxGetN(prhs[ON_SAME_TET]) != n_cells) mexErrMsgTxt("on_same_tet must be a n_cells X n_cells matrix"); on_same_tet = (int)mxGetPr(prhs[ON_SAME_TET]); /* create outputs */ n_combs = 0; for(i = 0; i < n_cells; i++) for(j = i+1; j < n_cells; j++) for(k = j+1; k < n_cells; k++) { /* int ix = lookup3[i] + (n_cells - i) *(j - i - 1) */ /* - (j - i - 1) * (j -i) /2 + (k-j-1); */ int i1, i2, i3; midx[0] = i; midx[1] = j; i1 = on_same_tet[mxCalcSingleSubscript(prhs[ON_SAME_TET], 2, midx)]; midx[0] = i; midx[1] = k; i2 = on_same_tet[mxCalcSingleSubscript(prhs[ON_SAME_TET], 2, midx)]; midx[0] = j; midx[1] = k; i3 = on_same_tet[mxCalcSingleSubscript(prhs[ON_SAME_TET], 2, midx)]; if(!(i1 || i2 || i3)) n_combs++; } mexPrintf("n_combs = %d\n", n_combs); plhs[TRIPLETS_FREQ] = mxCreateNumericMatrix(n_combs, 6, mxDOUBLE_CLASS, mxREAL); triplets_freq = mxGetPr(plhs[TRIPLETS_FREQ]); plhs[TRIPLES_TABLE] = mxCreateNumericMatrix(n_combs, 3, mxDOUBLE_CLASS, mxREAL); triples_table = mxGetPr(plhs[TRIPLES_TABLE]); plhs[TRIPLETS_TS] = mxCreateCellMatrix(n_combs, 6); plhs[TRIPLETS_X] = mxCreateCellMatrix(n_combs, 6); plhs[TRIPLETS_Y] = mxCreateCellMatrix(n_combs, 6); plhs[TRIPLETS_PHI] = mxCreateCellMatrix(n_combs, 6); /* create lookup tables direct and inverse */ triples_lookup = (int *) mxCalloc(n_cells*n_cells*n_cells, sizeof(int)); for(i = 0; i < n_cells*n_cells*n_cells; i++) triples_lookup[i] = -1; triplets_last_ts = (int **)mxCalloc(3, sizeof(int *)); for(i = 0; i < 3; i++) triplets_last_ts[i] = (int *)mxCalloc(n_combs, sizeof(int)); { int ix = 0; for(i = 0; i < n_cells; i++) for(j = i+1; j < n_cells; j++) for(k = j+1; k < n_cells; k++) { /* int ix = lookup3[i] + (n_cells - i) *(j - i - 1) */ /* - (j - i - 1) * (j -i) /2 + (k-j-1); */ int i1, i2, i3; midx[0] = i; midx[1] = j; i1 = on_same_tet[mxCalcSingleSubscript(prhs[ON_SAME_TET], 2, midx)]; midx[0] = i; midx[1] = k; i2 = on_same_tet[mxCalcSingleSubscript(prhs[ON_SAME_TET], 2, midx)]; midx[0] = j; midx[1] = k; i3 = on_same_tet[mxCalcSingleSubscript(prhs[ON_SAME_TET], 2, midx)]; if(!(i1 || i2 || i3)) { midx[0] = ix; midx[1] = 0; triples_table[mxCalcSingleSubscript(plhs[TRIPLES_TABLE], 2, midx)] = i+1; midx[1] = 1; triples_table[mxCalcSingleSubscript(plhs[TRIPLES_TABLE], 2, midx)] = j+1; midx[1] = 2; triples_table[mxCalcSingleSubscript(plhs[TRIPLES_TABLE], 2, midx)] = k+1; triples_lookup[lookup_idx(i,j,k)] = ix; ix++; } } } /* now complete the direct lookup table */ for(i = 0; i < n_cells; i++) for(j = 0; j < n_cells; j++) for(k = 0; k < n_cells; k++) { cidx[0] = i; cidx[1] = j; cidx[2] = k; order_tuple(cidx, 3); triples_lookup[lookup_idx(i,j,k)] = triples_lookup[lookup_idx(cidx[0], cidx[1], cidx[2])]; } /* and now compute the frequency table */ i_last = 0; for(i = 0; i < n_spikes-2; i++) { double t1 = tstamp[i]; double tl = t1 + time_span; int ix = idxs[i]; int jx, kx, cx, px; if(! (i % 10000)) mexPrintf("i: %d\n", i); while(i_last < n_spikes && tstamp[i_last] < tl) i_last++; for(j = i+1; j < i_last; j++) if(idxs[j] != ix) { jx = idxs[j]; for(k = j+1; k < i_last; k++) if(idxs[k] != ix && idxs[k] != jx) { kx = idxs[k]; cidx[0] = ix; cidx[1] = jx; cidx[2] = kx; px = order_code(cidx); order_tuple(cidx, 3); cx = triples_lookup[lookup_idx(cidx[0], cidx[1], cidx[2])]; if(cx >= 0 && (!omit_double || (triplets_last_ts[0][cx] != tstamp[i] && triplets_last_ts[1][cx] != tstamp[j] && triplets_last_ts[2][cx] != tstamp[k]))) { triplets_last_ts[0][cx] = tstamp[i]; triplets_last_ts[1][cx] = tstamp[j]; triplets_last_ts[2][cx] = tstamp[k]; n_triplets_true++; midx[0] = cx; midx[1] = px; triplets_freq[mxCalcSingleSubscript(plhs[TRIPLETS_FREQ], 2, midx)]++; } } } } /* now allocate memory for timestamps, x, y */ triplets_ts_ptr = (double ***)mxCalloc(6, sizeof(double **)); triplets_x_ptr = (double ***)mxCalloc(6, sizeof(double **)); triplets_y_ptr = (double ***)mxCalloc(6, sizeof(double **)); triplets_phi_ptr = (double ***)mxCalloc(6, sizeof(double **)); triplets_count = (int **)mxCalloc(6, sizeof(int *)); for(i = 0; i < 6; i++) { triplets_ts_ptr[i] = (double **)mxCalloc(n_combs, sizeof(double **)); triplets_x_ptr[i] = (double **)mxCalloc(n_combs, sizeof(double *)); triplets_y_ptr[i] = (double **)mxCalloc(n_combs, sizeof(double *)); triplets_phi_ptr[i] = (double **)mxCalloc(n_combs, sizeof(double *)); triplets_count[i] = (int *)mxCalloc(n_combs, sizeof(int)); } for(i = 0; i < n_combs; i++) for(j = 0; j < 6; j++) { int n_triplets; mxArray *tmp; midx[0] = i; midx[1] = j; n_triplets = triplets_freq[mxCalcSingleSubscript(plhs[TRIPLETS_FREQ], 2, midx)]; tmp = mxCreateNumericMatrix(n_triplets, 1, mxDOUBLE_CLASS, mxREAL); triplets_ts_ptr[j][i] = mxGetPr(tmp); mxSetCell(plhs[TRIPLETS_TS], mxCalcSingleSubscript(plhs[TRIPLETS_TS], 2, midx), tmp); n_triplets_chk += n_triplets; tmp = mxCreateNumericMatrix(n_triplets, 1, mxDOUBLE_CLASS, mxREAL); triplets_x_ptr[j][i] = mxGetPr(tmp); mxSetCell(plhs[TRIPLETS_X], mxCalcSingleSubscript(plhs[TRIPLETS_X], 2, midx), tmp); tmp = mxCreateNumericMatrix(n_triplets, 1, mxDOUBLE_CLASS, mxREAL); triplets_y_ptr[j][i] = mxGetPr(tmp); mxSetCell(plhs[TRIPLETS_Y], mxCalcSingleSubscript(plhs[TRIPLETS_Y], 2, midx), tmp); tmp = mxCreateNumericMatrix(n_triplets, 1, mxDOUBLE_CLASS, mxREAL); triplets_phi_ptr[j][i] = mxGetPr(tmp); mxSetCell(plhs[TRIPLETS_PHI], mxCalcSingleSubscript(plhs[TRIPLETS_PHI], 2, midx), tmp); } if (n_triplets_chk != n_triplets_true) { char errmsg[80]; sprintf(errmsg, "n_triplets = %d, n_triplets_chk = %d!!!", n_triplets_true, n_triplets_chk); mexErrMsgTxt(errmsg); } /* now fill timestamp and position arrays */ for (i = 0; i < 3; i++) for(j = 0; j < n_combs; j++) triplets_last_ts[i][j] = 0; i_last = 0; for(i = 0; i < n_spikes-2; i++) { double t1 = tstamp[i]; double tl = t1 + time_span; int ix = idxs[i]; int jx, kx, cx, px; if(! (i % 10000)) mexPrintf("i: %d\n", i); while(i_last < n_spikes && tstamp[i_last] < tl) i_last++; for(j = i+1; j < i_last; j++) if(idxs[j] != ix) { jx = idxs[j]; for(k = j+1; k < i_last; k++) if(idxs[k] != ix && idxs[k] != jx) { kx = idxs[k]; cidx[0] = ix; cidx[1] = jx; cidx[2] = kx; px = order_code(cidx); order_tuple(cidx, 3); cx = triples_lookup[lookup_idx(cidx[0], cidx[1], cidx[2])]; if(cx >= 0 && (!omit_double || (triplets_last_ts[0][cx] != tstamp[i] && triplets_last_ts[1][cx] != tstamp[j] && triplets_last_ts[2][cx] != tstamp[k]))) { triplets_last_ts[0][cx] = tstamp[i]; triplets_last_ts[1][cx] = tstamp[j]; triplets_last_ts[2][cx] = tstamp[k]; triplets_ts_ptr[px][cx][triplets_count[px][cx]] = tstamp[i]; triplets_x_ptr[px][cx][triplets_count[px][cx]] = x_pos[i]; triplets_y_ptr[px][cx][triplets_count[px][cx]] = y_pos[i]; triplets_phi_ptr[px][cx][triplets_count[px][cx]] = phi_pos[i]; triplets_count[px][cx]++; } } } } mxFree((void *)triples_lookup); for(i = 0; i < 6; i++) { mxFree((void *)triplets_ts_ptr[i]); mxFree((void *)triplets_x_ptr[i]); mxFree((void *)triplets_y_ptr[i]); mxFree((void *)triplets_phi_ptr[i]); mxFree((void *)triplets_count[i]); } for(i = 0; i < 3; i++) mxFree((void *)triplets_last_ts[i]); mxFree((void *)triplets_ts_ptr); mxFree((void *)triplets_x_ptr); mxFree((void *)triplets_y_ptr); mxFree((void *)triplets_phi_ptr); mxFree((void *)triplets_count); mxFree((void *)triplets_last_ts); /* mexCallMATLAB(0, NULL, 1, &plhs[TRIPLETS_PHI], "disp"); */ }
mwIndex MxArray::subs(const std::vector<mwIndex>& si) const { return mxCalcSingleSubscript(p_, si.size(), &si[0]); }
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { mxArray *A_cell_pr; double *A, *B; int *irA, *jcA, *irB, *jcB; double *Btmp, *tr; int isspA, isspB, iscellA, iscellB; int subs[2]; int nsubs=2; int mA, nA, m1, n1, m2, n2, j, index; int rowidx, colidx, r, k, kstart, kend; /* CHECK THE DIMENSIONS */ iscellA = mxIsCell(prhs[1]); mA = mxGetM(prhs[1]); if (!iscellA) { mA = 1; } if (nrhs < 2) { mexErrMsgTxt(" mexinprod: must have at least 3 inputs"); } if (nlhs>2) { mexErrMsgTxt("mexinprod: requires 1 output argument"); } if (nrhs > 4) { rowidx = (int)*mxGetPr(prhs[4]); } else { rowidx = 1; } if (rowidx > mA) { mexErrMsgTxt("mexinprod: rowidx exceeds size(Avec,1)"); } /***** assign pointers *****/ iscellB = mxIsCell(prhs[2]); isspB = mxIsSparse(prhs[2]); m2 = mxGetM(prhs[2]); n2 = mxGetN(prhs[2]); if ((n2 > 1) || (iscellB)) { mexErrMsgTxt("mexinprod: 3RD input must be a column vector"); } if (isspB) { irB = mxGetIr(prhs[2]); jcB = mxGetJc(prhs[2]); Btmp = mxGetPr(prhs[2]); /***** copy Btmp to B *****/ B = mxCalloc(m2,sizeof(double)); kstart = jcB[0]; kend = jcB[1]; for (k=kstart; k<kend; k++) { r = irB[k]; B[r] = Btmp[k]; } } else { B = mxGetPr(prhs[2]); } if (iscellA) { subs[0] = rowidx-1; /* subtract 1 to adjust for Matlab index */ subs[1] = 0; index = mxCalcSingleSubscript(prhs[1],nsubs,subs); A_cell_pr = mxGetCell(prhs[1],index); A = mxGetPr(A_cell_pr); m1 = mxGetM(A_cell_pr); n1 = mxGetN(A_cell_pr); isspA = mxIsSparse(A_cell_pr); if (isspA) { irA = mxGetIr(A_cell_pr); jcA = mxGetJc(A_cell_pr); } } else { A = mxGetPr(prhs[1]); m1 = mxGetM(prhs[1]); n1 = mxGetN(prhs[1]); isspA = mxIsSparse(prhs[1]); if (isspA) { irA = mxGetIr(prhs[1]); jcA = mxGetJc(prhs[1]); } } if (nrhs > 3) { colidx = (int)*mxGetPr(prhs[3]); } else { colidx = 1; } if (colidx > n1) { mexErrMsgTxt("mexinprod: colidx exceeds size(Avec,2)"); } if (m1 != m2) { mexErrMsgTxt("mexinprod: 2ND and 3RD input not compatible."); } /***** create return argument *****/ plhs[0] = mxCreateDoubleMatrix(colidx,1,mxREAL); tr = mxGetPr(plhs[0]); /***** compute <Aj,B> *****/ if (isspA) { for (j=0; j<colidx; j++){ tr[j] = realdot2(A,irA,jcA,j,B); } } else { for (j=0; j<colidx; j++){ tr[j] = realdot1(A,j,B,m1); } } if (isspB) { mxFree(B); } return; }
bool mat_load_multi_array_vec2(MATFile *f, QString qsVarName, std::vector<std::vector<Array> > &vvA) { const char *name = 0; bool bRes = false; vvA.clear(); if (f != 0) { mxArray *matlab_mat = matGetNextVariable(f, &name); std::cout << name << std::endl; bool bLoaded = false; while (matlab_mat != 0 && !bLoaded) { if (qsVarName == name) { if (mxIsCell(matlab_mat)) { mwSize cell_ndim = mxGetNumberOfDimensions(matlab_mat); const mwSize *cell_dims = mxGetDimensions(matlab_mat); //std::cout << "number of cell dimensions: " << cell_ndim << endl; assert(cell_ndim == 2); for (int dim_idx = 0; dim_idx < 2; ++dim_idx) { std::cout << "extent of cell dim " << dim_idx << ": " << cell_dims[dim_idx] << std::endl; } uint N1 = cell_dims[0]; uint N2 = cell_dims[1]; //vvA.resize(N1, std::vector<Array>(N2, Array())); for (uint i1 = 0; i1 < N1; ++i1) { vvA.push_back(std::vector<Array>()); for (uint i2 = 0; i2 < N2; ++i2) { /* get dimensions of the current cell */ mwIndex subs[2]; subs[0] = i1; subs[1] = i2; mwIndex cell_idx = mxCalcSingleSubscript(matlab_mat, cell_ndim, subs); mxArray *E = mxGetCell(matlab_mat, cell_idx); mwSize mat_ndim = mxGetNumberOfDimensions(E); const mwSize *mat_dims = mxGetDimensions(E); //std::cout << "number of cell element dimensions: " << mat_ndim << endl; assert(mat_ndim == Array::dimensionality); /* copy from matlab matrix to Array */ boost::array<typename Array::size_type, Array::dimensionality> array_shape; for (uint i = 0; i < Array::dimensionality; ++i) array_shape[i] = mat_dims[i]; Array B(array_shape, boost::fortran_storage_order()); uint nElements = B.num_elements(); if (nElements > 0) { typename Array::element *data2 = B.data(); if (mxIsSingle(E)) { float *pE = (float *)mxGetPr(E); assert(pE != 0); for (uint i = 0; i < nElements; ++i) { *(data2 + i) = *pE; ++pE; } } else { double *pE = mxGetPr(E); assert(pE != 0); for (uint i = 0; i < nElements; ++i) { *(data2 + i) = *pE; ++pE; } } //vvA[i1][i2] = B; /* copy to array with normal storage order */ // Array A = B; // this does not work!!! (storage order is of course copied at construction :) Array A(array_shape); A = B; vvA.back().push_back(A); } } }// cell elements bRes = true; }// is cell else { std::cout << "variable is not a cell array" << std::endl; } bLoaded = true; } mxDestroyArray(matlab_mat); matlab_mat = 0; if (!bLoaded) matlab_mat = matGetNextVariable(f, &name); }// variables } assert(bRes && "variable not found or could not open file"); return bRes; }
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[]) { int nsubs, index, x; double *temp; int *subs; /* Check for proper number of input and output arguments */ if (nrhs != 2) { mexErrMsgTxt("Two input arguments required."); } if (nlhs > 1) { mexErrMsgTxt("Too many output arguments."); } /* Check data type of first input argument */ if (!mxIsDouble(prhs[0])) { mexErrMsgTxt("First input argument must be a double."); } /* Check data type of first second argument */ if (!mxIsDouble(prhs[0]) || mxIsComplex(prhs[0])) { mexErrMsgTxt("Second input argument must be a real double."); } /* Get the number of dimensions in array */ nsubs=mxGetNumberOfDimensions(prhs[0]); /* Check for the correct number of indices */ if (mxGetNumberOfElements(prhs[1]) != nsubs){ mexErrMsgTxt("You must specify an index for each dimension."); } /* Allocate memory for the subs array on the fly */ subs=mxCalloc(nsubs,sizeof(int)); /* Get the indices and account for the fact that MATLAB is 1 based and C is zero based. While doing this, check to make sure that an index was not specified that is larger than size of input array */ temp=mxGetPr(prhs[1]); for (x=0;x<nsubs;x++){ subs[x]=(int)temp[x]-1; if (temp[x]> ((mxGetDimensions(prhs[0]))[x]) ){ mxFree(subs); mexErrMsgTxt("You indexed above the size of the array."); } } /* Find the index of location selected. Note, for example, that (3,4) in MATLAB corresponds to (2,3) in C. */ index = mxCalcSingleSubscript(prhs[0], nsubs, subs); /* Create the output array */ plhs[0] = mxCreateDoubleMatrix(1, 1, mxIsComplex(prhs[0]) ? mxCOMPLEX : mxREAL); /* Free allocated memory*/ mxFree(subs); /* Assign value in C based array to plhs. */ mxGetPr(plhs[0])[0]= mxGetPr(prhs[0])[index]; if (mxIsComplex(prhs[0])) { mxGetPi(plhs[0])[0]= mxGetPi(prhs[0])[index]; } }
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { mwSize subs[2]; mxArray *trowmajor; char *zsql, *strbuf; int i, j, k, iret, istep, irow=0, nrow_alloc=0, ncol, kval, nval, kk, iargbind=4; int isnumeric=1, ldestroy=1, coltyp, classid; int n_par_tobind=0, n_val_tobind=1; const mxArray *mxBindPar=NULL; strcat(errmsgstr1, "select:"); /* check for proper number of arguments */ if( nrhs<2) ERRABORT("nrhs", "at least two input arguments are needed."); /* Make sure that input arguments 2...2nd last are strings: */ for( k=2; k<nrhs-1; k++ ) if( !mxIsChar(prhs[k]) ) ERRABORT("notChar", "After the 2nd argument only string input is expected."); /* if the first argument is a string, then open a database connection */ if( mxIsChar(prhs[0]) ) { mexCallMATLAB(1, &mxppDb, 1, (mxArray **) &prhs[0], "sql_open"); ppDb = *((sqlite3 **) mxGetData(mxppDb)); } else ppDb = *((sqlite3 **) mxGetData(prhs[0])); zsql = getzsql(nrhs, prhs, &iargbind); /* mexPrintf("zsql = %s\n", zsql); */ /* Prepare the SQL library for the select statement: */ iret = sqlite3_prepare_v2(ppDb, zsql, strlen(zsql), &ppStmt, NULL); /* mexPrintf("sqlite3_prepare_v2 returned %d\n", iret); */ /* Return the statement in the second optional output argument*/ if( nlhs>=2 ) plhs[1] = mxCreateString(zsql); else mxFree(zsql); if( iret!=SQLITE_OK ) { char errmsgbuf[strlen(sqlite3_errmsg(ppDb))+1]; strcpy(errmsgbuf, sqlite3_errmsg(ppDb)); CLOSE_DB; ERRABORT("prepare", errmsgbuf); } /* If there are parameters to bind, check the matlab variables and find out how many parameters/values: */ bind_m2sql(ppStmt, iargbind, prhs, &n_par_tobind, &n_val_tobind); /* How many columns? */ ncol = sqlite3_column_count(ppStmt); /* mexPrintf("sqlite3_column_count returned %d\n", ncol); */ /* The numbers of rows is not known at this point. Therefore the elements are first stored row-by-row in a temporary cell array, so that memory can be reallocated as database rows are added. */ trowmajor = mxCreateCellMatrix(ncol, 0); /* Loop over the values that need to be bound to parameters, if no parameters to bind n_val_tobind=1 and the loop is executed once: */ for( kval=0; kval<n_val_tobind; kval++ ) { BIND_MATPAR(MEXFUN) /* Step along the selected rows: */ while( (istep = sqlite3_step(ppStmt))==SQLITE_ROW ) { /* A row is available, if needed then enlarge the output cell array: */ if( irow>=nrow_alloc ) { /* mexPrintf("irow = %d, nrow_alloc = %d\n", irow, nrow_alloc);*/ if( nrow_alloc==0 ) /* The first row is found */ nrow_alloc++; else nrow_alloc *= 2; mxSetData(trowmajor, mxRealloc(mxGetData(trowmajor), sizeof(mxArray *)*nrow_alloc*ncol)); /* mexPrintf("%d bytes reallocated\n", sizeof(mtype)*nrow_alloc*ncol); */ } /* Increment the nr of columns of the output matrix by one: */ mxSetN(trowmajor, mxGetN(trowmajor)+1); /* mexPrintf("plhs enlarged to %d %d\n", mxGetM(trowmajor), mxGetN(trowmajor)); */ subs[1] = irow++; for( k=0; k<ncol; k++ ) { subs[0] = k; coltyp = sqlite3_column_type(ppStmt, k); /* mexPrintf("subs = %d %d, coltyp = %d, mxCalcSingleSubscript = %d\n", subs[0], subs[1], coltyp, mxCalcSingleSubscript(trowmajor, 2, subs)); */ switch( coltyp) { mxArray *rnum; int nblob; case SQLITE_NULL: /* NULL values are mapped in Matlab to empty cells: */ /* mexPrintf("NULL selected at ncol = %d and irow %d\n", subs[0], subs[1]); */ rnum = mxCreateDoubleMatrix(0, 1, mxREAL); mxSetCell(trowmajor, mxCalcSingleSubscript(trowmajor, 2, subs), rnum); isnumeric = 0; break; case SQLITE_TEXT: mxSetCell(trowmajor, mxCalcSingleSubscript(trowmajor, 2, subs), mxCreateString(sqlite3_column_text(ppStmt, k))); isnumeric = 0; break; case SQLITE_BLOB: nblob = sqlite3_column_bytes(ppStmt, k); mxArray *blob = mxCreateNumericMatrix(1, nblob, mxUINT8_CLASS, mxREAL); memcpy(mxGetPr(blob), sqlite3_column_blob(ppStmt, k), nblob); mxSetCell(trowmajor, mxCalcSingleSubscript(trowmajor, 2, subs), blob); isnumeric = 0; break; case SQLITE_FLOAT: rnum = mxCreateDoubleMatrix(1, 1, mxREAL); *mxGetPr(rnum) = sqlite3_column_double(ppStmt, k); mxSetCell(trowmajor, mxCalcSingleSubscript(trowmajor, 2, subs), rnum); break; default: sqlite3_finalize(ppStmt); CLOSE_DB; ERRABORT("column_type", "db should not have this column type"); } } } /* mexPrintf("last return from sqlite3_step was %d\n", istep); */ if( istep!=SQLITE_DONE ) { char errmsgbuf[strlen(sqlite3_errmsg(ppDb))+1]; strcpy(errmsgbuf, sqlite3_errmsg(ppDb)); sqlite3_finalize(ppStmt); CLOSE_DB; ERRABORT("done", sqlite3_errmsg(ppDb)); } iret = sqlite3_reset(ppStmt); if( iret!=SQLITE_OK ) mexWarnMsgIdAndTxt("Sqlite4m:sql_insert:reset", sqlite3_errmsg(ppDb)); } TRY_SQLFUN(sqlite3_finalize(ppStmt), select, finalize); /* Release memory to the actual nr of db rows/matlab columns: */ mxSetData(trowmajor, mxRealloc(mxGetData(trowmajor), sizeof(mxArray *)*mxGetN(trowmajor)*ncol)); /* mexPrintf("final allocation is %d bytes\n", sizeof(mtype)*mxGetN(plhs[0])*ncol); */ /* if the first argument was a string, then close the database connection */ CLOSE_DB; /* Transpose the row-by-row cell array into a double matrix if all elements are numeric: */ /* if( isnumeric ) { */ /* plhs[0] = mxCreateDoubleMatrix(mxGetN(trowmajor), */ /* mxGetM(trowmajor), mxREAL); */ /* /\* Traverse the temporary row major array column by column, */ /* then the output array can be filled linearly: *\/ */ /* k = 0; */ /* for( i=0; i<mxGetM(trowmajor); i++ ) { */ /* subs[0] = i; */ /* for( j=0; j<mxGetN(trowmajor); j++ ) { */ /* subs[1] = j; */ /* mxGetPr(plhs[0])[k++] = */ /* *mxGetPr(mxGetCell(trowmajor, */ /* mxCalcSingleSubscript(trowmajor, 2, subs))); */ /* } */ /* } */ /* mxDestroyArray(trowmajor); */ /* /\* or else use Matlab transpose to copy into the output cell array, */ /* if at least one element is text or a blob: *\/ */ /* } else */ mexCallMATLAB(1, plhs, 1, &trowmajor, "transpose"); }
mwIndex MxArray::subs(const std::vector<mwIndex>& si) const { return mxCalcSingleSubscript(p_, si.size(), (!si.empty() ? &si[0] : NULL)); }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { mxArray *blk_cell_pr; double *A, *B, *AI, *BI, *blksize; mwIndex *irA, *jcA, *irB, *jcB; int *cumblksize, *blknnz; int mblk, isspA, isspB, iscmpA; mwIndex subs[2]; mwSize nsubs=2; int m, n, n2, nsub, k, index, numblk, NZmax, type; double r2; /* CHECK FOR PROPER NUMBER OF ARGUMENTS */ if (nrhs < 2){ mexErrMsgTxt("mexsvec: requires at least 2 input arguments."); } if (nlhs > 1){ mexErrMsgTxt("mexsvec: requires 1 output argument."); } /* CHECK THE DIMENSIONS */ mblk = mxGetM(prhs[0]); if (mblk > 1) { mexErrMsgTxt("mexsvec: blk can have only 1 row."); } m = mxGetM(prhs[1]); n = mxGetN(prhs[1]); if (m != n) { mexErrMsgTxt("mexsvec: matrix must be square."); } subs[0] = 0; subs[1] = 1; index = mxCalcSingleSubscript(prhs[0],nsubs,subs); blk_cell_pr = mxGetCell(prhs[0],index); numblk = mxGetN(blk_cell_pr); blksize = mxGetPr(blk_cell_pr); if (numblk == 1) { n2 = n*(n+1)/2; } else { cumblksize = mxCalloc(numblk+1,sizeof(int)); blknnz = mxCalloc(numblk+1,sizeof(int)); cumblksize[0] = 0; blknnz[0] = 0; n = 0; n2 = 0; for (k=0; k<numblk; ++k) { nsub = (int) blksize[k]; n += nsub; n2 += nsub*(nsub+1)/2; cumblksize[k+1] = n; blknnz[k+1] = n2; } } /***** assign pointers *****/ A = mxGetPr(prhs[1]); isspA = mxIsSparse(prhs[1]); iscmpA = mxIsComplex(prhs[1]); if (isspA) { irA = mxGetIr(prhs[1]); jcA = mxGetJc(prhs[1]); NZmax = mxGetNzmax(prhs[1]); } else { NZmax = n2; } if (iscmpA) { AI = mxGetPi(prhs[1]); } if ((numblk > 1) & (!isspA)) { mexErrMsgTxt("mexsvec: matrix must be sparse for numblk > 1"); } if (nrhs > 2) { if (mxGetM(prhs[2])>1) { isspB = (int)*mxGetPr(prhs[2]); } else if (NZmax < n2/2) { isspB = 1; } else { isspB = 0; } } else { if (NZmax < n2/2) { isspB = 1; } else { isspB = 0; } } if (nrhs > 3) { type = (int)*mxGetPr(prhs[3]); } else { type = 0; } /***** create return argument *****/ if (isspB) { if (iscmpA) { plhs[0] = mxCreateSparse(n2,1,NZmax,mxCOMPLEX); } else { plhs[0] = mxCreateSparse(n2,1,NZmax,mxREAL); } B = mxGetPr(plhs[0]); irB = mxGetIr(plhs[0]); jcB = mxGetJc(plhs[0]); jcB[0] = 0; } else { if (iscmpA) { plhs[0] = mxCreateDoubleMatrix(n2,1,mxCOMPLEX); } else { plhs[0] = mxCreateDoubleMatrix(n2,1,mxREAL); } B = mxGetPr(plhs[0]); } if (iscmpA) { BI = mxGetPi(plhs[0]); } /***** Do the computations in a subroutine *****/ r2 = sqrt(2); if (type == 0) { if (iscmpA) { if (numblk == 1) { svec1cmp(n,r2,A,irA,jcA,isspA,B,irB,jcB,isspB,AI,BI); } else { svec2cmp(n,numblk,cumblksize,blknnz,r2,A,irA,jcA,isspA,B,irB,jcB,isspB,AI,BI); } } else { if (numblk == 1) { svec1(n,r2,A,irA,jcA,isspA,B,irB,jcB,isspB); } else { svec2(n,numblk,cumblksize,blknnz,r2,A,irA,jcA,isspA,B,irB,jcB,isspB); } } } else { if (iscmpA) { if (numblk == 1) { svec3cmp(n,r2,A,irA,jcA,isspA,B,irB,jcB,isspB,AI,BI); } else { svec4cmp(n,numblk,cumblksize,blknnz,r2,A,irA,jcA,isspA,B,irB,jcB,isspB,AI,BI); } } else { if (numblk == 1) { svec3(n,r2,A,irA,jcA,isspA,B,irB,jcB,isspB); } else { svec4(n,numblk,cumblksize,blknnz,r2,A,irA,jcA,isspA,B,irB,jcB,isspB); } } } return; }
void formatMatrix(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { /* if (nlhs < 1) { mexWarnMsgTxt("formatMatrix did not get an output argument, so not doing any real work. Specify an output argument to do real work."); RETURN_NULL(); }*/ if (nrhs != 1 || !mxIsCell(*prhs) || !mxGetNumberOfDimensions(*prhs)) mexErrMsgTxt("formatMatrix: needs one input argument of type cell"); const int m = mxGetM(*prhs), n = mxGetN(*prhs); int i,j; static long outbuf_len = 1024*1024; // preallocate 1MB char *outbuf = (char *)mxCalloc(outbuf_len, sizeof(char)), *outbufPtr = outbuf; // preallocate 1MB for output if (!outbuf) mexErrMsgTxt("formatMatrix: failed to allocate 1MB for output txt"); for (i = 0; i < m; ++i) { for (j = 0; j < n; ++j) { int subs[2] = { i, j }; char buf[4096]; mxArray *cell = mxGetCell(*prhs, mxCalcSingleSubscript(*prhs, 2, subs)); if (!cell) { mxFree(outbuf); mexErrMsgTxt("formatMatrix: A cell element in the cell array could not be retrieved!"); } void *data = mxGetData(cell); if (!data) { // can happen on empty array mxFree(outbuf); mexErrMsgTxt("formatMatrix: A cell element in the cell array is empty/null -- all elements need to contain data!"); } switch(mxGetClassID(cell)) { case mxCHAR_CLASS: mxGetString(cell, buf, sizeof(buf)); buf[sizeof(buf)-1] = 0; break; case mxINT8_CLASS: sprintf(buf, "%hhd", *(char *)mxGetData(cell)); break; case mxUINT8_CLASS: sprintf(buf, "%hhu", *(unsigned char *)mxGetData(cell)); break; case mxINT16_CLASS: sprintf(buf, "%hd", *(short *)mxGetData(cell)); break; case mxUINT16_CLASS: sprintf(buf, "%hu", *(unsigned short *)mxGetData(cell)); break; case mxINT32_CLASS: sprintf(buf, "%d", *(int *)mxGetData(cell)); break; case mxUINT32_CLASS: sprintf(buf, "%u", *(unsigned int *)mxGetData(cell)); break; case mxSINGLE_CLASS: sprintf(buf, "%g", *(float *)mxGetData(cell)); break; case mxDOUBLE_CLASS: sprintf(buf, "%g", *(double *)mxGetData(cell)); break; default: mxFree(outbuf); mexErrMsgTxt("formatMatrix: Unknown cell type encountered!"); } long lenSoFar = outbufPtr - outbuf; if (lenSoFar + (strlen(buf)*3+4) >= outbuf_len) { // reallocate if ran out of space void * tmp = mxRealloc(outbuf, outbuf_len*=2); if (!tmp) { mxFree(outbuf); mexErrMsgTxt("formatMatrix: Out of space in output text buffer and failed to allocate more!"); } outbuf = (char *)tmp; outbufPtr = outbuf + lenSoFar; } // it's 1 cell per line.... *outbufPtr++ = ' '; *outbufPtr++ = ' '; outbufPtr += UrlEncode(outbufPtr, buf); *outbufPtr++ = '\n'; } } // return the text.. if (plhs[0]) mxDestroyArray(plhs[0]); plhs[0] = mxCreateString(outbuf); mxFree(outbuf); }
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[]) { double *m_w_d, *p_w_z, *p_z_d, *p_w_d; mwIndex *ir, *jc; size_t d, k; size_t n_w, n_d, n_z; mwSize ndim = 1, dims[1]; mwSize nsubs = 1; mxArray *temp_array; double *temp; size_t beg_row_id, end_row_id, cur_row_id; mwIndex index, subs[1]; mwIndex *beg_of_ir, *beg_of_jc; mwSize nzmax; size_t total = 0; if(!mxIsSparse(prhs[0])) { printf("word-doc matrix should be sparse matrix\n"); return; } else if(nrhs != 4 || nlhs != 1) { printf("usage: p_z_wd = mex_Estep_sparse(m_w_d, p_w_z_n, p_z_d_n, p_w_d)\n"); return; } m_w_d = mxGetPr(prhs[0]); jc = mxGetJc(prhs[0]); ir = mxGetIr(prhs[0]); nzmax = mxGetNzmax(prhs[0]); n_w = mxGetM(prhs[0]); n_d = mxGetN(prhs[0]); p_w_z = mxGetPr(prhs[1]); n_z = mxGetN(prhs[1]); p_z_d = mxGetPr(prhs[2]); p_w_d = mxGetPr(prhs[3]); dims[0] = n_z; plhs[0] = mxCreateCellArray(ndim, dims); //total_num_of_cells = mxGetNumberOfElements(plhs[0]); for(k = 0; k < n_z; k++) { total = 0; subs[0] = k; index = mxCalcSingleSubscript(plhs[0], nsubs, subs); temp_array = mxCreateSparse(n_w, n_d, nzmax, mxREAL); temp = mxGetPr(temp_array); mxSetNzmax(temp_array, nzmax); //Place ir data into the newly created sparse array. beg_of_ir = mxGetIr(temp_array); memcpy(beg_of_ir, ir, nzmax * sizeof(mwIndex)); //Place jc data into the newly created sparse array. beg_of_jc = mxGetJc(temp_array); memcpy(beg_of_jc, jc, (n_d + 1) * sizeof(mwIndex)); for (d = 0; d < n_d; d++) { beg_row_id = jc[d]; end_row_id = jc[d + 1]; if (beg_row_id == end_row_id) continue; else { for (cur_row_id = beg_row_id; cur_row_id < end_row_id; cur_row_id++) { temp[total] = p_w_z[k * n_w + ir[cur_row_id]] * p_z_d[d * n_z + k] / p_w_d[total]; total++; } } } mxSetCell(plhs[0], index, temp_array); } return; }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { int number_of_arrays = (nrhs - 2) / 2; double *index_sizes = mxGetPr(prhs[0]); int number_of_indices = mxGetM(prhs[0]) * mxGetN(prhs[0]); int output_ndims = mxGetM(prhs[1]) * mxGetN(prhs[1]); int *output_dims; double *output; int k; int *indices; int number_index_values = 1; int x; int max_array_dims = output_ndims; int *array_indices; int output_index; double p; int m; /* Create the result array. * * In case all indices are repeated the output is a scalar which * should logically be considered to be zero-dimensional, but that * doesn't go so well with Matlab's model of arrays. Instead we * have to special case it. */ if (output_ndims == 0) plhs[0] = mxCreateDoubleScalar(0.0); else { output_dims = mxCalloc(output_ndims, sizeof(output_dims[0])); for (k = 0; k < output_ndims; k++) { int n = (int) mxGetPr(prhs[1])[k] - 1; output_dims[k] = (int) mxGetPr(prhs[0])[n]; } plhs[0] = mxCreateNumericArray(output_ndims, output_dims, mxDOUBLE_CLASS, mxREAL); mxFree(output_dims); } output = mxGetPr(plhs[0]); /* Allocate space to hold the index values and find out the total * number of index combinations to loop over. */ indices = mxCalloc(number_of_indices, sizeof(indices[0])); for (k = 0; k < number_of_indices; k++) number_index_values *= index_sizes[k]; /* Find the maximum number of indices for a single array. */ for (m = 0; m < number_of_arrays; m++) if (mxGetNumberOfDimensions(prhs[m + 2]) > max_array_dims) max_array_dims = mxGetNumberOfDimensions(prhs[m + 2]); /* Allocate space for the index values used by a single array. * This is shared between all arrays. */ array_indices = mxCalloc(max_array_dims, sizeof(array_indices[0])); /* Loop over all combinations of index values. */ for (x = 0; x < number_index_values; x++) { /* Compute index values for this iteration of the loop. */ int y = x; for (k = 0; k < number_of_indices; k++) { indices[k] = y % (int) index_sizes[k]; y /= index_sizes[k]; } /* Find the linear subscript into the output array. Once more * the scalar output case must be special-cased. */ if (output_ndims == 0) output_index = 0; else { for (k = 0; k < output_ndims; k++) array_indices[k] = indices[(int) mxGetPr(prhs[1])[k] - 1]; output_index = mxCalcSingleSubscript(plhs[0], output_ndims, array_indices); } /* Compute the product of the array values for the current set * of indices. */ p = 1.0; for (m = 0; m < number_of_arrays; m++) { int n = mxGetM(prhs[2 + number_of_arrays + m]); double *i = mxGetPr(prhs[2 + number_of_arrays + m]); int index; for (k = 0; k < n; k++) array_indices[k] = indices[(int) i[k] - 1]; index = mxCalcSingleSubscript(prhs[2 + m], n, array_indices); p *= mxGetPr(prhs[2 + m])[index]; } /* Sum this product to the result. */ output[output_index] += p; } mxFree(indices); mxFree(array_indices); }
/* MEX interface function */ void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { FreemanPyramidCntrlStruct *cntrl = NULL; FreemanPyramidStruct *fp = NULL; FreemanCacheCntrlStruct *fpcc = NULL; float *image, *tmp; float epsAmp = 0.0; float epsC = 0.0; int nxImage, nyImage; int iChannel, iScale, iOrient; int iCell, iOut, iBasis; int k, n; mwSize dims[3]; mxArray *mxTmp; /* check for the required input arguments */ if( nrhs < 2 || nrhs > 3 ) mexErrMsgTxt( "Input expected: CNTRL, IMAGE, CACHECNTRL" ); /* decode the matlab version of the pyramid control structure and allocate the pyramid structure */ mexFreemanPyramidCntrlStruct( prhs[0], &cntrl, &epsAmp, &epsC ); fp = newFreemanPyramidStruct( cntrl ); if( nrhs == 3 ) mexFreemanCacheCntrlStruct( prhs[2], &fpcc ); /* get the image to be filtered */ mex2float( prhs[1], &image, &nxImage, &nyImage ); /* build the pyramid */ if( fpcc == NULL ) { computeFreemanPyramid_Float(fp, image, nxImage, nyImage, epsAmp, epsC); } else { recoverFreemanPyramid_Float(fp, image, nxImage, nyImage, epsAmp, epsC, fpcc ); } /* return the results to matlab in the order they appear in cntrl->storeMap */ iOut = 0; /* return the gradient maps if necessary */ if( cntrl->storeMap[isGrad] ) { if( iOut >= nlhs ) mexErrMsgTxt( "Too few output arguments" ); dims[0] = cntrl->nScale; dims[1] = cntrl->nOrient; dims[2] = 2; plhs[iOut] = mxCreateCellArray( 3, dims ); for( iScale = 0; iScale < cntrl->nScale; iScale++ ) { for( iOrient = 0; iOrient < cntrl->nOrient; iOrient++ ) { iChannel = 2*(cntrl->nOrient*iScale + iOrient); dims[0] = iScale; dims[1] = iOrient; float2mex( fp->gradMap[iChannel], &mxTmp, fp->nxFilt[iScale], fp->nyFilt[iScale] ); dims[2] = 0; iCell = mxCalcSingleSubscript( plhs[iOut], 3, dims ); mxSetCell( plhs[iOut], iCell, mxTmp ); float2mex( fp->gradMap[iChannel+1], &mxTmp, fp->nxFilt[iScale], fp->nyFilt[iScale] ); dims[2] = 1; iCell = mxCalcSingleSubscript( plhs[0], 3, dims ); mxSetCell( plhs[iOut], iCell, mxTmp ); } } iOut++; } /* return the phase maps if necessary */ if( cntrl->storeMap[isPhase] ) { if( iOut >= nlhs ) mexErrMsgTxt( "Too few output arguments" ); dims[0] = cntrl->nScale; dims[1] = cntrl->nOrient; plhs[iOut] = mxCreateCellArray( 2, dims ); for( iScale = 0; iScale < cntrl->nScale; iScale++ ) { for( iOrient = 0; iOrient < cntrl->nOrient; iOrient++ ) { iChannel = cntrl->nOrient*iScale + iOrient; dims[0] = iScale; dims[1] = iOrient; float2mex( fp->phaseMap[iChannel], &mxTmp, fp->nxFilt[iScale], fp->nyFilt[iScale] ); iCell = mxCalcSingleSubscript( plhs[iOut], 2, dims ); mxSetCell( plhs[iOut], iCell, mxTmp ); } } iOut++; } /* return the amplitude maps if necessary */ if( cntrl->storeMap[isAmp] ) { if( iOut >= nlhs ) mexErrMsgTxt( "Too few output arguments" ); dims[0] = cntrl->nScale; dims[1] = cntrl->nOrient; plhs[iOut] = mxCreateCellArray( 2, dims ); for( iScale = 0; iScale < cntrl->nScale; iScale++ ) { for( iOrient = 0; iOrient < cntrl->nOrient; iOrient++ ) { iChannel = cntrl->nOrient*iScale + iOrient; dims[0] = iScale; dims[1] = iOrient; float2mex( fp->ampMap[iChannel], &mxTmp, fp->nxFilt[iScale], fp->nyFilt[iScale] ); iCell = mxCalcSingleSubscript( plhs[iOut], 2, dims ); mxSetCell( plhs[iOut], iCell, mxTmp ); } } iOut++; } /* return the filter maps if necessary */ if( cntrl->storeMap[isFilter] ) { if( iOut >= nlhs ) mexErrMsgTxt( "Too few output arguments" ); dims[0] = cntrl->nScale; dims[1] = cntrl->nOrient; dims[2] = 2; plhs[iOut] = mxCreateCellArray( 3, dims ); for( iScale = 0; iScale < cntrl->nScale; iScale++ ) { for( iOrient = 0; iOrient < cntrl->nOrient; iOrient++ ) { iChannel = 2*(cntrl->nOrient*iScale + iOrient); dims[0] = iScale; dims[1] = iOrient; float2mex( fp->filterMap[iChannel], &mxTmp, fp->nxFilt[iScale], fp->nyFilt[iScale] ); dims[2] = 0; iCell = mxCalcSingleSubscript( plhs[iOut], 3, dims ); mxSetCell( plhs[iOut], iCell, mxTmp ); float2mex( fp->filterMap[iChannel+1], &mxTmp, fp->nxFilt[iScale], fp->nyFilt[iScale] ); dims[2] = 1; iCell = mxCalcSingleSubscript( plhs[0], 3, dims ); mxSetCell( plhs[iOut], iCell, mxTmp ); } } iOut++; } /* return the basis maps if necessary */ if( cntrl->storeMap[isBasis] ) { if( iOut >= nlhs ) mexErrMsgTxt( "Too few output arguments" ); dims[0] = cntrl->nScale; dims[1] = NUMBASIS; plhs[iOut] = mxCreateCellArray( 2, dims ); for( iScale = 0; iScale < cntrl->nScale; iScale++ ) { for( iBasis = 0; iBasis < NUMBASIS; iBasis++ ) { iChannel = NUMBASIS*iScale + iBasis; dims[0] = iScale; dims[1] = iBasis; float2mex( fp->basisMap[iChannel], &mxTmp, fp->nxFilt[iScale], fp->nyFilt[iScale] ); iCell = mxCalcSingleSubscript( plhs[iOut], 2, dims ); mxSetCell( plhs[iOut], iCell, mxTmp ); } } iOut++; } /* return the pyramid maps if necessary */ if( cntrl->storeMap[isPyr] ) { if( iOut >= nlhs ) mexErrMsgTxt( "Too few output arguments" ); dims[0] = cntrl->nPyr; plhs[iOut] = mxCreateCellArray( 1, dims ); for( iScale = 0; iScale < cntrl->nPyr; iScale++ ) { dims[0] = iScale; float2mex( fp->pyrMap[iScale], &mxTmp, fp->nxPyr[iScale], fp->nyPyr[iScale] ); iCell = mxCalcSingleSubscript( plhs[iOut], 1, dims ); mxSetCell( plhs[iOut], iCell, mxTmp ); } iOut++; } /* free the pyramid, etc. */ freeFreemanPyramid( fp ); utilFree( (void **)&fp ); utilFree( (void **)&cntrl ); utilFree( (void **)&fpcc ); return; }
bool mat_save_multi_array_vec2(MATFile *f, QString qsVarName, const std::vector<std::vector<Array> > &vvA) { assert(f != 0); mwSize cell_ndim = 2; mwSize cell_dims[2]; assert(vvA.size() > 0); uint N1 = vvA.size(); uint N2 = vvA[0].size(); cell_dims[0] = N1; cell_dims[1] = N2; mxArray *CA = mxCreateCellArray(cell_ndim, cell_dims); assert(CA != 0); for (uint i1 = 0; i1 < N1; ++i1) { assert(vvA.size() > i1); assert(vvA[i1].size() == N2); for (uint i2 = 0; i2 < N2; ++i2) { /* init shape array */ const typename Array::size_type *shape_ptr = vvA[i1][i2].shape(); assert(shape_ptr != 0); boost::array<typename Array::size_type, Array::dimensionality> array_shape; for (uint i = 0; i < Array::dimensionality; ++i) array_shape[i] = shape_ptr[i]; /* create array of the same shape as initial array with data stored in fortran storage order */ Array B(array_shape, boost::fortran_storage_order()); B = vvA[i1][i2]; const typename Array::element *data2 = B.data(); assert(data2 != 0); /* shape again, this time as mwSize array */ mwSize dims[Array::dimensionality]; for (uint i = 0; i < Array::dimensionality; ++i) dims[i] = array_shape[i]; mxArray *E = mxCreateNumericArray(Array::dimensionality, dims, mxSINGLE_CLASS, mxREAL); assert(E != 0); /* copy elements to matlab array */ size_t nElements = vvA[i1][i2].num_elements(); if (nElements > 0) { float *pE = (float *)mxGetPr(E); assert(pE != 0); for (uint idx = 0; idx < nElements; ++idx) { *pE = *(data2 + idx); ++pE; } } /* add element to cell array */ mwIndex subs[2]; subs[0] = i1; subs[1] = i2; mwIndex cell_idx = mxCalcSingleSubscript(CA, cell_ndim, subs); //std::cout << "cell index: " << cell_idx << std::endl; mxSetCell(CA, cell_idx, E); } } matPutVariable(f, qsVarName.toStdString().c_str(), CA); mxDestroyArray(CA); return true; }