mxArray* write_score_plif_struct(const struct score_plif_struct *SCR, int &M, int &N) { // fprintf(stderr, "M: %i N: %i field_names ", M, N) ; const int nfields = 4; const char *field_names[] = {"limits", "scores", "dim", "w"}; mxArray *ret = mxCreateStructMatrix(M, N, nfields, field_names); int n_elems = mxGetNumberOfElements(ret); for(int j=0; j<n_elems; ++j) { int len = SCR[j].len; const int dims[] = {1,len}; mxArray *lim = mxCreateNumericArray(2, (const mwSize*) dims, mxDOUBLE_CLASS, mxREAL); double *lim_ptr = mxGetPr(lim); memcpy(lim_ptr, SCR[j].limits, len*mxGetElementSize(lim)); mxSetField(ret, j, field_names[0], lim); mxArray *scr = mxCreateNumericArray(2,(const mwSize*) dims, mxDOUBLE_CLASS, mxREAL); double *scr_ptr = mxGetPr(scr); memcpy(scr_ptr, SCR[j].scores, len*mxGetElementSize(scr)); mxSetField(ret, j, field_names[1], scr); mxArray *dim = mxCreateDoubleScalar(SCR[j].feat_idx); mxSetField(ret, j, field_names[2], dim); mxArray *wght = mxCreateNumericArray(2, (const mwSize*) dims, mxDOUBLE_CLASS, mxREAL); double *w_ptr = mxGetPr(wght); memcpy(w_ptr, SCR[j].weights, len*mxGetElementSize(wght)); mxSetField(ret, j, field_names[3], wght); } return ret; }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { int *ret; int sock; int len = -1; int cnt = 0; unsigned char *cdata; /* Allocate a return variable */ plhs[0] = mxCreateNumericMatrix(1,1,mxINT32_CLASS,mxREAL); ret = (int *)mxGetPr(plhs[0]); ret[0] = -1; if(nrhs < 2) { mexPrintf("Must input a socket and a variable.\n"); return; } if(!mxIsNumeric(prhs[0])) { mexPrintf("First argument must be a socket.\n"); return; } if(!mxIsNumeric(prhs[1])) { mexPrintf("send variable must be numeric.\n"); return; } /* Query the length to send */ if(nrhs > 2) { if(!mxIsNumeric(prhs[2])) { mexPrintf("Length must be numeric.\n"); return; } len = (int)mxGetScalar(prhs[2]); if(len < 0) return; if(len > (int) (mxGetNumberOfElements(prhs[1])*mxGetElementSize(prhs[1]))) len = (int) (mxGetNumberOfElements(prhs[1])*mxGetElementSize(prhs[1])); } else len = (int) (mxGetNumberOfElements(prhs[1])*mxGetElementSize(prhs[1])); sock = (int)mxGetScalar(prhs[0]); /* Send the string */ cdata = (unsigned char *)mxGetPr(prhs[1]); while(cnt < len) { ret[0] = send(sock,cdata+cnt,len-cnt,0); if(ret[0] == -1) { perror("send"); return; } cnt += ret[0]; } return; } /* end of mexFunction */
void toInt32(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { const mxArray *inArray; if(nrhs != 1 || !prhs || !(inArray = *prhs) || !mxIsNumeric(inArray) || mxIsSparse(inArray)) mexErrMsgTxt("Input matrix must be a non-sparse numeric array!"); //if(nlhs < 1) mexErrMsgTxt("One output argument required."); mxArray * & outArray = plhs[0]; const int m = mxGetM(inArray), n = mxGetN(inArray); const int dims[] = { m, n }; outArray = mxCreateNumericArray(2, dims, mxINT32_CLASS, mxREAL); const unsigned elem_size = mxGetElementSize(inArray); GenericMatrix in(mxGetData(inArray), elem_size, m, n), out(mxGetData(outArray), mxGetElementSize(outArray), m, n); int class_id = mxGetClassID(inArray); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; j++) { void *inptr = static_cast<void *>( in[i][j] ); int32 & outval = *reinterpret_cast<int32 *>(static_cast<void *>(out[i][j])); switch(class_id) { case mxINT8_CLASS: outval = *reinterpret_cast<int8 *>(inptr); break; case mxUINT8_CLASS: outval = *reinterpret_cast<uint8 *>(inptr); break; case mxINT16_CLASS: outval = *reinterpret_cast<int16 *>(inptr); break; case mxUINT16_CLASS: outval = *reinterpret_cast<uint16 *>(inptr); break; case mxINT32_CLASS: outval = *reinterpret_cast<int32 *>(inptr); break; case mxUINT32_CLASS: outval = *reinterpret_cast<uint32 *>(inptr); break; default: /* assume double.. */ outval = static_cast<int32>(*reinterpret_cast<double *>(inptr) * std::numeric_limits<int32>::max()); break; } } } }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { long nnod, nnod_reg, reg; double *inreg; double *source; double *nodes_reg; double D; size_t elt_size; mxArray *q; if(nrhs != 7) mexErrMsgTxt("7 inputs required\n"); if(nlhs != 1) mexErrMsgTxt("1 output required\n"); nnod = (long) *(mxGetPr(prhs[0])); elt_size = mxGetElementSize(prhs[1]); assert(elt_size != 0); nnod_reg = mxGetM(prhs[1]); nodes_reg = mxGetPr(prhs[1]); if(mxGetN(prhs[2]) != 1) mexErrMsgTxt("Need row vector for inreg\n "); inreg = mxGetPr(prhs[2]); mxArray *omega = mxCreateDoubleMatrix(1,1,mxCOMPLEX); *(mxGetPr(omega)) = *(mxGetPr(prhs[3])); if (mxIsComplex(prhs[3])) *(mxGetPi(omega)) = *(mxGetPi(prhs[3])); else *(mxGetPi(omega)) = 0.; D = *(mxGetPr(prhs[4])); elt_size = mxGetElementSize(prhs[5]), assert(elt_size != 0); source = mxGetPr(prhs[5]); reg = (long) *mxGetPr(prhs[6]); q = mxCreateDoubleMatrix(nnod, 1, mxCOMPLEX); build_source(nodes_reg, inreg, nnod, nnod_reg, omega, D, source, reg, q); plhs[0] = q; mxDestroyArray(omega); }
cv::Mat MxArray::toMat(int depth, bool transpose) const { // Create cv::Mat object (of the specified depth), equivalent to mxArray std::vector<int> d(dims(), dims()+ndims()); const mwSize ndims = (d.size()>2) ? d.size()-1 : d.size(); const mwSize nchannels = (d.size()>2) ? d.back() : 1; depth = (depth == CV_USRTYPE1) ? DepthOf[classID()] : depth; std::swap(d[0], d[1]); cv::Mat mat(ndims, &d[0], CV_MAKETYPE(depth, nchannels)); // Copy each channel from mxArray to Mat (converting to specified depth), // as in: channels[i] <- cast_to_mat_depth(p_(:,:,i)) std::vector<cv::Mat> channels(nchannels); std::vector<mwSize> si(d.size(), 0); // subscript index const int type = CV_MAKETYPE(DepthOf[classID()], 1); // Source type for (mwIndex i = 0; i<nchannels; ++i) { si[si.size() - 1] = i; // last dim is a channel idx void *pd = reinterpret_cast<void*>( reinterpret_cast<size_t>(mxGetData(p_)) + mxGetElementSize(p_)*subs(si)); // ptr to i-th channel data const cv::Mat m(ndims, &d[0], type, pd); // only creates Mat headers // Read from mxArray through m, writing into channels[i] m.convertTo(channels[i], CV_MAKETYPE(depth, 1)); } // Merge channels back into one cv::Mat array cv::merge(channels, mat); // transpose cv::Mat if needed if (mat.dims==2 && transpose) mat = mat.t(); return mat; }
cv::Mat MxArray::toMat(int depth, bool transpose) const { // Create cv::Mat object. std::vector<int> d(dims(), dims()+ndims()); int ndims = (d.size()>2) ? d.size()-1 : d.size(); int nchannels = (d.size()>2) ? *(d.end()-1) : 1; depth = (depth==CV_USRTYPE1) ? DepthOf[classID()] : depth; std::swap(d[0], d[1]); cv::Mat mat(ndims, &d[0], CV_MAKETYPE(depth, nchannels)); // Copy each channel. std::vector<cv::Mat> channels(nchannels); std::vector<mwSize> si(d.size(), 0); // subscript index int type = CV_MAKETYPE(DepthOf[classID()], 1); // Source type for (int i = 0; i<nchannels; ++i) { si[d.size()-1] = i; void *pd = reinterpret_cast<void*>( reinterpret_cast<size_t>(mxGetData(p_))+ mxGetElementSize(p_)*subs(si)); cv::Mat m(ndims, &d[0], type, pd); // Read from mxArray through m m.convertTo(channels[i], CV_MAKETYPE(depth, 1)); } cv::merge(channels, mat); return (mat.dims==2 && transpose) ? cv::Mat(mat.t()) : mat; }
void setInfoFieldsFromMx(mxGramInfo *info, const mxArray *mx) { info->gramType = (MX_GRAM_UINT16)getMxGramTypeForMx(mx); info->dataSize = (MX_GRAM_UINT16)mxGetElementSize(mx); info->dataM = (MX_GRAM_UINT16)mxGetM(mx); info->dataN = (MX_GRAM_UINT16)mxGetN(mx); info->dataLength = (MX_GRAM_UINT16)(info->dataSize*info->dataM*info->dataN); }
int readMxCharDataFromBytes(mxArray *mx, mxGramInfo *info, int nBytes) { int ii; int numel = info->dataM * info->dataN; mxChar* mxData = mxGetChars(mx); const char *gramData = info->dataBytes; if (nBytes >= mxGetElementSize(mx)*numel) { if (info->dataSize == 1) { // read 1-byte characters from data bytes for(ii=0; ii<numel; ii++) { mxData[ii] = *gramData; gramData += info->dataSize; } } else { // read 2-byte characters from data bytes for(ii=0; ii<numel; ii++) { mxData[ii] = *((mxChar*)gramData); gramData += info->dataSize; } } return(info->dataSize*numel); } else return(-1); }
void interleaveMatrix(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { const mxArray *inArray; if(nrhs != 1 || !prhs || !(inArray = *prhs) || !mxIsNumeric(inArray) || mxIsSparse(inArray)) mexErrMsgTxt("Input matrix must be a non-sparse numeric array!"); //if(nlhs < 1) mexErrMsgTxt("One output argument required."); mxArray * & outArray = plhs[0]; const int m = mxGetM(inArray), n = mxGetN(inArray); const int dims[] = { 1, m*n }; outArray = mxCreateNumericArray(2, dims, mxGetClassID(inArray), mxREAL); const unsigned elem_size = mxGetElementSize(inArray); /* GenericMatrix in(mxGetData(inArray), elem_size, m, n), out(mxGetData(outArray), elem_size, 1, m*n); for (int i = 0; i < m; ++i) for (int j = 0; j < n; j++) out[0][i*n + j] = static_cast<void *>( in[i][j] );*/ // NB: you can just do a memcpy since all matrices are stored in column-major order anyway.. ::memcpy(mxGetData(outArray), mxGetData(inArray), elem_size*m*n); }
/// mxMemCpyDtoD void GPUmxMemCpyHtoD(const GPUtype &DST, int nrhs, const mxArray *prhs[]) { // dst_index is given in Matlab format, starting from 1 // we convert to C int dst_index = (int) mxGetScalar(prhs[1]) - 1; int count = (int) mxGetScalar(prhs[2]); // retrieve DST properties gpuTYPE_t dst_type = gm->gputype.getType(DST); const void * dst_gpuptr = gm->gputype.getGPUptr(DST); int dst_numel = gm->gputype.getNumel(DST); int dst_dsize = gm->gputype.getDataSize(DST); // retrieve SRC properties gpuTYPE_t src_type; if (mxIsSingle(prhs[0])) { src_type = gpuFLOAT; if (mxIsComplex(prhs[0])) { //src_type = gpuCFLOAT; mexErrMsgTxt(ERROR_MEMCPYHTOD_MXCOMPLEXNOTSUPP); } } else if (mxIsDouble(prhs[0])) { src_type = gpuDOUBLE; if (mxIsComplex(prhs[0])) { //src_type = gpuCDOUBLE; mexErrMsgTxt(ERROR_MEMCPYHTOD_MXCOMPLEXNOTSUPP); } } const void * src_gpuptr = mxGetPr(prhs[0]); int src_numel = mxGetNumberOfElements(prhs[0]); int src_dsize = mxGetElementSize(prhs[0]); // do some checks if ((dst_index>=dst_numel)||(dst_index<0)) mexErrMsgTxt(ERROR_MEMCPYHTOD_WRONGDSTINDEX); if ((dst_index+count)>dst_numel) mexErrMsgTxt(ERROR_MEMCPYHTOD_TOOMANYEL); if (src_type != dst_type) mexErrMsgTxt(ERROR_MEMCPYHTOD_DSTSRCSAME); // calculate destination int offsetdst = dst_dsize*dst_index; void *dst = (void*) ((UINTPTR dst_gpuptr)+offsetdst); // source const void *src = src_gpuptr; cudaError_t cudastatus = cudaSuccess; cudastatus = cudaMemcpyAsync(dst, src, count*dst_dsize, cudaMemcpyHostToDevice, 0); if (cudastatus != cudaSuccess) { mexErrMsgTxt("cudaMemcpy error"); } }
void mexCopy2DIntArray(int * A, mxArray * mexA, int dim0, int dim1) { unsigned char * start_of_pr = (unsigned char *)mxGetData(mexA); size_t bytes_to_copy = dim0 * mxGetElementSize(mexA); for (int i=0; i<dim1; i++) { memcpy(start_of_pr, &(A[i]), bytes_to_copy); start_of_pr += bytes_to_copy; } }
/* The gateway routine */ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { /* ----------------- Variables ----------------- */ /* input variables */ /* mandatory input */ int startIdx; int endIdx; double * inReal; /* optional input */ /* output variables */ int outBegIdx; int outNbElement; int* outInteger; /* input dimentions */ int inSeriesRows; int inSeriesCols; /* error handling */ TA_RetCode retCode; /* ----------------- input/output count ----------------- */ /* Check for proper number of arguments. */ if (nrhs < 1 || nrhs > 1) mexErrMsgTxt("#1 inputs possible #0 optional."); if (nlhs != 1) mexErrMsgTxt("#1 output required."); /* ----------------- INPUT ----------------- */ /* Create a pointer to the input matrix inReal. */ inReal = mxGetPr(prhs[0]); /* Get the dimensions of the matrix input inReal. */ inSeriesCols = mxGetN(prhs[0]); if (inSeriesCols != 1) mexErrMsgTxt("inReal only vector alowed."); inSeriesRows = mxGetM(prhs[0]); endIdx = inSeriesRows - 1; startIdx = 0; /* Process optional arguments */ /* ----------------- OUTPUT ----------------- */ outInteger = mxCalloc(inSeriesRows, sizeof(int)); /* -------------- Invocation ---------------- */ retCode = TA_HT_TRENDMODE( startIdx, endIdx, inReal, &outBegIdx, &outNbElement, outInteger); /* -------------- Errors ---------------- */ if (retCode) { mxFree(outInteger); mexPrintf("%s%i","Return code=",retCode); mexErrMsgTxt(" Error!"); } // Populate Output plhs[0] = mxCreateNumericMatrix(outBegIdx+outNbElement,1, mxINT32_CLASS, mxREAL); memcpy(((int *) mxGetData(plhs[0]))+outBegIdx, outInteger, outNbElement*mxGetElementSize(plhs[0])); mxFree(outInteger); } /* END mexFunction */
mxArray *rt_mxDuplicateArray(const mxArray *pa) { /*LINTED E_ASSIGN_INT_TO_SMALL_INT*/ size_t nbytes = (mxGetNumberOfElements(pa)+2)*mxGetElementSize(pa); mxArray *pcopy = (mxArray *)malloc(nbytes); if (pcopy!=NULL) { (void)memcpy(pcopy, pa, nbytes); } return(pcopy); } /* end mxDuplicateArray */
int readMxLogicalDataFromBytes(mxArray *mx, mxGramInfo *info, int nBytes) { int ii; int numel = info->dataM * info->dataN; mxLogical *mxData = mxGetLogicals(mx); const char *gramData = info->dataBytes; if (nBytes >= mxGetElementSize(mx)*numel) { for(ii=0; ii<numel; ii++) { mxData[ii] = (mxLogical)*gramData; gramData += info->dataSize; } return(info->dataSize*numel); } else return(-1); }
int readMxDoubleDataFromBytes(mxArray *mx, mxGramInfo *info, int nBytes) { int ii; int numel = info->dataM * info->dataN; double* mxData = mxGetPr(mx); const char *gramData = info->dataBytes; if (nBytes >= mxGetElementSize(mx)*numel) { // read 8-byte doubles from data bytes for(ii=0; ii<numel; ii++) { mxData[ii] = readDouble64FromBytes(gramData); gramData += info->dataSize; } return(info->dataSize*numel); } else return(-1); }
MxArray::MxArray(const cv::Mat& mat, mxClassID classid, bool transpose) { // handle special case of empty input Mat by creating an empty array classid = (classid == mxUNKNOWN_CLASS) ? ClassIDOf[mat.depth()] : classid; if (mat.empty()) { p_ = mxCreateNumericArray(0, 0, classid, mxREAL); if (!p_) mexErrMsgIdAndTxt("mexopencv:error", "Allocation error"); return; } // transpose cv::Mat if needed cv::Mat input(mat); if (input.dims == 2 && transpose) input = input.t(); // Create a new mxArray (of the specified classID) equivalent to cv::Mat const mwSize nchannels = input.channels(); const int* dims_ = input.size; std::vector<mwSize> d(dims_, dims_ + input.dims); d.push_back(nchannels); std::swap(d[0], d[1]); if (classid == mxLOGICAL_CLASS) { // OpenCV's logical true is any nonzero, while MATLAB's true is 1. cv::compare(input, 0, input, cv::CMP_NE); input.setTo(1, input); p_ = mxCreateLogicalArray(d.size(), &d[0]); } else p_ = mxCreateNumericArray(d.size(), &d[0], classid, mxREAL); if (!p_) mexErrMsgIdAndTxt("mexopencv:error", "Allocation error"); // split input cv::Mat into several single-channel arrays std::vector<cv::Mat> channels; channels.reserve(nchannels); cv::split(input, channels); // Copy each channel from Mat to mxArray (converting to specified classid), // as in: p_(:,:,i) <- cast_to_classid_type(channels[i]) std::vector<mwSize> si(d.size(), 0); // subscript index const int type = CV_MAKETYPE(DepthOf[classid], 1); // destination type for (mwIndex i = 0; i < nchannels; ++i) { si[si.size() - 1] = i; // last dim is a channel index void *ptr = reinterpret_cast<void*>( reinterpret_cast<size_t>(mxGetData(p_)) + mxGetElementSize(p_) * subs(si)); // ptr to i-th channel data cv::Mat m(input.dims, dims_, type, ptr); // only creates Mat header channels[i].convertTo(m, type); // Write to mxArray through m } }
/* * sendData: Sends data back to a Matlab argument. * Inputs: * mxArray **plhs: Left hand side argument to get edge detected image * (image *) Image to go back to Matlab. */ int sendData( mxArray **plhs, image *ed_im ) { double *start_of_pr; int bytes_to_copy, elements; elements = ed_im->dims[0] * ed_im->dims[1]; /* Create a dims[0] by dims[1] array of unsigned 8-bit integers. */ *plhs = mxCreateNumericArray(NDIMS,ed_im->dims,mxDOUBLE_CLASS,mxREAL); /* Populate the the created array. */ start_of_pr = (double *) mxGetData(*plhs); bytes_to_copy = ( elements ) * mxGetElementSize(*plhs); memcpy(start_of_pr, ed_im->im, bytes_to_copy); return SUCCESS; }
/* * sendData: Sends data back to a Matlab argument. * Inputs: * mxArray **plhs: Left hand side argument to get BW image * (image8 *) Image struct to go back to Matlab. */ void sendData(mxArray **plhs, image8 *im) { uint8 *start_of_pr; int bytes_to_copy, elements; elements = im->dims[0] * im->dims[1]; // Create a dims[0] by dims[1] array of unsigned 8-bit integers. *plhs = mxCreateNumericArray(NDIMS,im->dims,mxUINT8_CLASS,mxREAL); // Populate the the created array. start_of_pr = (uint8 *) mxGetData(*plhs); bytes_to_copy = elements * mxGetElementSize(*plhs); memcpy(start_of_pr, im->im, bytes_to_copy); return; }
MxArray::MxArray(const cv::Mat& mat, mxClassID classid, bool transpose) { if (mat.empty()) { p_ = mxCreateNumericArray(0, 0, mxDOUBLE_CLASS, mxREAL); if (!p_) mexErrMsgIdAndTxt("mexopencv:error", "Allocation error"); return; } cv::Mat input = (mat.dims == 2 && transpose) ? mat.t() : mat; // Create a new mxArray. const int nchannels = input.channels(); const int* dims_ = input.size; std::vector<mwSize> d(dims_, dims_ + input.dims); d.push_back(nchannels); classid = (classid == mxUNKNOWN_CLASS) ? ClassIDOf[input.depth()] : classid; std::swap(d[0], d[1]); if (classid == mxLOGICAL_CLASS) { // OpenCV's logical true is any nonzero while matlab's true is 1. cv::compare(input, 0, input, cv::CMP_NE); input.setTo(1, input); p_ = mxCreateLogicalArray(d.size(), &d[0]); } else { p_ = mxCreateNumericArray(d.size(), &d[0], classid, mxREAL); } if (!p_) mexErrMsgIdAndTxt("mexopencv:error", "Allocation error"); // Copy each channel. std::vector<cv::Mat> channels; split(input, channels); std::vector<mwSize> si(d.size(), 0); // subscript index. int type = CV_MAKETYPE(DepthOf[classid], 1); // destination type. for (int i = 0; i < nchannels; ++i) { si[si.size() - 1] = i; // last dim is a channel index. void *ptr = reinterpret_cast<void*>( reinterpret_cast<size_t>(mxGetData(p_)) + mxGetElementSize(p_) * subs(si)); cv::Mat m(input.dims, dims_, type, ptr); channels[i].convertTo(m, type); // Write to mxArray through m. } }
mxArray *mxCreateNumericArrayE(mwSize ndim, const mwSize *dims, mxClassID classid, mxComplexity ComplexFlag) { mxArray *a; mwSize i; mwSize *dims1 = mxMalloc(ndim*sizeof(mwSize)); mwSize sz = 1; for(i=0;i<ndim;i++) { sz *= dims[i]; dims1[i] = 1; } a = mxCreateNumericArray(ndim,dims1,classid,ComplexFlag); sz *= mxGetElementSize(a); mxSetDimensions(a, dims, ndim); mxFree(dims1); mxSetData(a, mxRealloc(mxGetData(a), sz)); if(ComplexFlag == mxCOMPLEX) { mxSetPi(a, mxRealloc(mxGetPi(a),sz)); } return a; }
/* the gataway function */ void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { const mwSize dims[]={2,2}; unsigned char *start_of_pr; unsigned short data[]={1,2,3,4}; size_t bytes_to_copy; (void) nlhs; (void) nrhs; (void) prhs; /* unused parameters */ /* call the computational subroutine */ dbl_elem(data); /* create a 2-by-2 array of unsigned 16-bit integers */ plhs[0] = mxCreateNumericArray(NDIMS,dims,mxUINT16_CLASS,mxREAL); /* populate the real part of the created array */ start_of_pr = (unsigned char *)mxGetData(plhs[0]); bytes_to_copy = TOTAL_ELEMENTS * mxGetElementSize(plhs[0]); memcpy(start_of_pr,data,bytes_to_copy); }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { double *A, *B; /* pointers to input matrices */ double *B2; /* in/out arguments to DGESV*/ size_t m,n; /* matrix dimensions */ mwSignedIndex *iPivot; /* inputs to DGESV */ mwSignedIndex info; size_t uno; char *car; uno = 1; car = 'N'; /* Check for proper number of arguments. */ if ( nrhs != 3) { mexErrMsgIdAndTxt("MATLAB:matrixDivide64:rhs", "This function requires 3 input matrices."); } A = mxGetPr(prhs[0]); /* pointer to first input matrix */ B = mxGetPr(prhs[1]); /* pointer to second input matrix */ iPivot = (mwSignedIndex*)mxGetData(prhs[2]); /* dimensions of input matrices */ m = mxGetM(prhs[0]); n = mxGetN(prhs[0]); /* Validate input arguments */ if (n != m) { mexErrMsgIdAndTxt("MATLAB:matrixDivide64:square", "LAPACK function requires input matrix 1 must be square."); } plhs[0] = mxCreateDoubleMatrix(m, 1, mxREAL); B2 = mxGetPr(plhs[0]); memcpy(B2, B, m*mxGetElementSize(prhs[1])); /* Call LAPACK */ dgetrs(&car, &n, &uno, A, &m, iPivot, B2, &m, &info); /* plhs[0] now holds X */ }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { /* Matlab call: X = solve_chol(A, B) */ doublereal *C; integer n, m, q; if (nrhs != 2 || nlhs > 1) /* check input */ mexErrMsgTxt("Usage: X = solve_chol(A, B)"); n = mxGetM(prhs[0]); /* number of rows in inputs A and B */ if (n != mxGetN(prhs[0])) mexErrMsgTxt("First argument matrix must be square."); if (n != mxGetM(prhs[1])) mexErrMsgTxt("Both argument matrices must have the same number of rows."); m = mxGetN(prhs[1]); /* number of colums in second input B */ plhs[0] = mxCreateDoubleMatrix(n, m, mxREAL); /* space for output X */ C = mxGetPr(plhs[0]); if (n == 0) return; /* if argument was empty matrix, do no more */ memcpy( C, mxGetPr(prhs[1]), m*n*mxGetElementSize(plhs[0]) ); /* copy data */ dpotrs("U", &n, &m, mxGetPr(prhs[0]), &n, C, &n, &q); /* solve system */ if (q < 0) mexErrMsgTxt("Error: illegal input to solve_chol"); }
mxArray* convertMatrix2mxArray(matrix vdata) { int m = vdata.size(); int n = vdata.nobs(); // array temporaneo con le osservazioni double *data = (double *) malloc (m * n * sizeof (double)); matrix::iterator p=vdata.begin(),e=vdata.end(); int i = 0; for (;p!=e;p++) { for (int j=0; j<n;j++) { data[j*m+i] = (*p)[j]; } i++; } mxArray *returned =mxCreateDoubleMatrix(m,n, mxREAL); size_t bytes_to_copy = m*n * (size_t) mxGetElementSize (returned); unsigned char *start_of_pr = (unsigned char *) mxGetData (returned); memcpy (start_of_pr, data, bytes_to_copy); free (data); return returned; }
void mexFunction(int nOut, mxArray *pOut[], int nIn, const mxArray *pIn[]) { mwSize width, height; float* borders; float* orientations; int* textons; unsigned int *in_image; mwSize dims[3]; mwSize orientation_dims[3]; if((nIn != 1) || (nOut != 3)) mexErrMsgTxt("Usage: border,textons,orientations = gpb(image)"); if (!mxIsClass(pIn[0],"uint8") || mxGetNumberOfDimensions(pIn[0]) != 3) { mexErrMsgTxt("Usage: th argument must be a unsigned int matrix"); } const mwSize *indims= mxGetDimensions(pIn[0]); if (indims[0]!=4) mexErrMsgTxt("Image needs to be of shape 4 x widht x height"); width = indims[2]; height = indims[1]; mexPrintf("width %d, height %d\n",width,height); mexPrintf("Element-size: %d, sizeof(int): %d, sizeof(char) %d\n",mxGetElementSize(pIn[0]),sizeof(int),sizeof(unsigned char)); dims[0]=width; dims[1]=height; //for rgb0 mexPrintf("width: %d height: %d\n",width, height); pOut[0]=mxCreateNumericMatrix(height,width,mxSINGLE_CLASS,mxREAL); pOut[1]=mxCreateNumericMatrix(height,width,mxINT32_CLASS,mxREAL); orientation_dims[0]=width; orientation_dims[1]=height; orientation_dims[2]=8; pOut[2]=mxCreateNumericArray(3,orientation_dims,mxSINGLE_CLASS,mxREAL); borders=(float*) mxGetPr(pOut[0]); textons=(int*) mxGetPr(pOut[1]); orientations=(float*) mxGetPr(pOut[2]); in_image = (unsigned int*) mxGetData(pIn[0]); gpb(in_image,height,width,borders,textons,orientations); }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { unsigned char *x, *y; int dims[2]; int m, n, elementSize; /* Check arguments */ if (nrhs < 1) mexErrMsgTxt("Need one input arguments."); x = (unsigned char *) mxGetData(prhs[0]); m = mxGetM(prhs[0]); n = mxGetN(prhs[0]); elementSize = mxGetElementSize(prhs[0]); dims[0] = m; dims[1] = n*elementSize; plhs[0] = mxCreateNumericArray(2,dims,mxUINT8_CLASS,mxREAL); y = (unsigned char *) mxGetData(plhs[0]); memcpy(y, x, m*n*elementSize); }
/* The gateway routine */ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { /* ----------------- Variables ----------------- */ /* input variables */ /* mandatory input */ int startIdx; int endIdx; double * high; double * low; double * close; /* optional input */ int optInFirstPeriod; int optInSecondPeriod; int optInThirdPeriod; /* output variables */ int outBegIdx; int outNbElement; double* outReal; /* input dimentions */ int inSeriesRows; int inSeriesCols; /* error handling */ TA_RetCode retCode; /* ----------------- input/output count ----------------- */ /* Check for proper number of arguments. */ if (nrhs < 3 || nrhs > 6) mexErrMsgTxt("#6 inputs possible #3 optional."); if (nlhs != 1) mexErrMsgTxt("#1 output required."); /* ----------------- INPUT ----------------- */ /* Create a pointer to the input matrix high. */ high = mxGetPr(prhs[0]); /* Get the dimensions of the matrix input high. */ inSeriesCols = mxGetN(prhs[0]); if (inSeriesCols != 1) mexErrMsgTxt("high only vector alowed."); /* Create a pointer to the input matrix low. */ low = mxGetPr(prhs[1]); /* Get the dimensions of the matrix input low. */ inSeriesCols = mxGetN(prhs[1]); if (inSeriesCols != 1) mexErrMsgTxt("low only vector alowed."); /* Create a pointer to the input matrix close. */ close = mxGetPr(prhs[2]); /* Get the dimensions of the matrix input close. */ inSeriesCols = mxGetN(prhs[2]); if (inSeriesCols != 1) mexErrMsgTxt("close only vector alowed."); inSeriesRows = mxGetM(prhs[2]); endIdx = inSeriesRows - 1; startIdx = 0; /* Process optional arguments */ if (nrhs >= 3+1) { if (!mxIsDouble(prhs[3]) || mxIsComplex(prhs[3]) || mxGetN(prhs[3])*mxGetM(prhs[3]) != 1) mexErrMsgTxt("Input optInFirstPeriod must be a scalar."); /* Get the scalar input optInTimePeriod. */ optInFirstPeriod = (int) mxGetScalar(prhs[3]); } else { optInFirstPeriod = 7; } if (nrhs >= 4+1) { if (!mxIsDouble(prhs[4]) || mxIsComplex(prhs[4]) || mxGetN(prhs[4])*mxGetM(prhs[4]) != 1) mexErrMsgTxt("Input optInSecondPeriod must be a scalar."); /* Get the scalar input optInTimePeriod. */ optInSecondPeriod = (int) mxGetScalar(prhs[4]); } else { optInSecondPeriod = 14; } if (nrhs >= 5+1) { if (!mxIsDouble(prhs[5]) || mxIsComplex(prhs[5]) || mxGetN(prhs[5])*mxGetM(prhs[5]) != 1) mexErrMsgTxt("Input optInThirdPeriod must be a scalar."); /* Get the scalar input optInTimePeriod. */ optInThirdPeriod = (int) mxGetScalar(prhs[5]); } else { optInThirdPeriod = 28; } /* ----------------- OUTPUT ----------------- */ outReal = mxCalloc(inSeriesRows, sizeof(double)); /* -------------- Invocation ---------------- */ retCode = TA_ULTOSC( startIdx, endIdx, high, low, close, optInFirstPeriod, optInSecondPeriod, optInThirdPeriod, &outBegIdx, &outNbElement, outReal); /* -------------- Errors ---------------- */ if (retCode) { mxFree(outReal); mexPrintf("%s%i","Return code=",retCode); mexErrMsgTxt(" Error!"); } // Populate Output plhs[0] = mxCreateDoubleMatrix(outBegIdx+outNbElement,1, mxREAL); memcpy(((double *) mxGetData(plhs[0]))+outBegIdx, outReal, outNbElement*mxGetElementSize(plhs[0])); mxFree(outReal); } /* END mexFunction */
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { const mxArray *srcmat; int ndim, *dimsize, eltsize; const int *dims; int ndimdest, *destdims, *destdimsize; char *src, *dest; int *rep; int i,nrep; int extra_rep = 1; int empty; if(nrhs < 2) mexErrMsgTxt("Usage: xrepmat(A, [N M ...])"); srcmat = prhs[0]; if(mxIsSparse(srcmat)) { mexErrMsgTxt("Sorry, can't handle sparse matrices yet."); } if(mxIsCell(srcmat)) { mexErrMsgTxt("Sorry, can't handle cell arrays yet."); } ndim = mxGetNumberOfDimensions(srcmat); dims = mxGetDimensions(srcmat); eltsize = mxGetElementSize(srcmat); /* compute dimension sizes */ dimsize = mxCalloc(ndim, sizeof(int)); dimsize[0] = eltsize*dims[0]; for(i=1;i<ndim;i++) dimsize[i] = dimsize[i-1]*dims[i]; /* determine repetition vector */ ndimdest = ndim; if(nrhs == 2) { nrep = mxGetN(prhs[1]); if(nrep > ndimdest) ndimdest = nrep; rep = mxCalloc(ndimdest, sizeof(int)); for(i=0;i<nrep;i++) { double repv = mxGetPr(prhs[1])[i]; rep[i] = (int)repv; } if(nrep == 1) { /* special behavior */ nrep = 2; rep[1] = rep[0]; } } else { nrep = nrhs-1; if(nrep > ndimdest) ndimdest = nrep; rep = mxCalloc(ndimdest, sizeof(int)); for(i=0;i<nrep;i++) { rep[i] = (int)*mxGetPr(prhs[i+1]); } } for(i=nrep;i<ndimdest;i++) rep[i] = 1; /* compute output size */ destdims = mxCalloc(ndimdest, sizeof(int)); for(i=0;i<ndim;i++) destdims[i] = dims[i]*rep[i]; for(;i<ndimdest;i++) { destdims[i] = rep[i]; extra_rep *= rep[i]; } destdimsize = mxCalloc(ndim, sizeof(int)); destdimsize[0] = eltsize*destdims[0]; for(i=1;i<ndim;i++) destdimsize[i] = destdimsize[i-1]*destdims[i]; /* for speed, array should be uninitialized */ plhs[0] = mxCreateNumericArray(ndimdest, destdims, mxGetClassID(srcmat), mxIsComplex(srcmat)?mxCOMPLEX:mxREAL); /* if any rep[i] == 0, output should be empty array. Added by KPM 11/13/02. */ empty = 0; for (i=0; i < nrep; i++) { if (rep[i]==0) empty = 1; } if (empty) return; src = (char*)mxGetData(srcmat); dest = (char*)mxGetData(plhs[0]); repmat(dest,src,ndim,destdimsize,dimsize,dims,rep); if(ndimdest > ndim) memrep(dest,destdimsize[ndim-1],extra_rep); if(mxIsComplex(srcmat)) { src = (char*)mxGetPi(srcmat); dest = (char*)mxGetPi(plhs[0]); repmat(dest,src,ndim,destdimsize,dimsize,dims,rep); if(ndimdest > ndim) memrep(dest,destdimsize[ndim-1],extra_rep); } }
void mexFunction ( /* === Parameters ======================================================= */ int nlhs, /* number of left-hand sides */ mxArray *plhs [], /* left-hand side matrices */ int nrhs, /* number of right--hand sides */ const mxArray *prhs [] /* right-hand side matrices */ ) { Dmat A; DILUPACKparam param; const char **fnames; /* pointers to field names */ const mwSize *dims; mxClassID *classIDflags; mwSize buflen, mrows, ncols, ndim; mxArray *tmp, *fout, *A_input, *options_input, *options_output; char *pdata, *input_buf, *output_buf; int ifield, status, nfields; size_t sizebuf; double dbuf; if (nrhs != 2) mexErrMsgTxt("Two input arguments required."); else if (nlhs > 1) mexErrMsgTxt("Too many output arguments."); else if (!mxIsStruct(prhs[1])) mexErrMsgTxt("Second input must be a structure."); else if (!mxIsNumeric(prhs[0])) mexErrMsgTxt("First input must be a matrix."); /* The first input must be a square matrix.*/ A_input=(mxArray *)prhs[0]; mrows = mxGetM(A_input); ncols = mxGetN(A_input); if (mrows!=ncols) { mexErrMsgTxt("First input must be a square matrix."); } A.nc=A.nr=mrows; A.ia=A.ja=NULL; A.a=NULL; DSYMAMGinit(&A,¶m); /* Get second input arguments */ options_input=(mxArray *)prhs[1]; nfields = mxGetNumberOfFields(options_input); /* Allocate memory for storing pointers */ fnames = mxCalloc((size_t)nfields, (size_t)sizeof(*fnames)); /* Allocate memory for storing classIDflags */ classIDflags = (mxClassID *) mxCalloc((size_t)nfields, (size_t)sizeof(mxClassID)); /* Get field name pointers */ for (ifield = 0; ifield < nfields; ifield++) { fnames[ifield] = mxGetFieldNameByNumber(options_input,ifield); } /* Create a 1x1 struct matrix for output */ nlhs=1; plhs[0] = mxCreateStructMatrix((mwSize)1, (mwSize)1, nfields, fnames); options_output=plhs[0]; /* copy data */ for (ifield = 0; ifield < nfields; ifield++) { tmp = mxGetFieldByNumber(options_input,(mwIndex)0,ifield); classIDflags[ifield] = mxGetClassID(tmp); ndim = mxGetNumberOfDimensions(tmp); dims = mxGetDimensions(tmp); /* Create string/numeric array */ if (classIDflags[ifield] == mxCHAR_CLASS) { /* Get the length of the input string. */ buflen = (mxGetM(tmp) * mxGetN(tmp)) + 1; if (!strcmp("amg",fnames[ifield])) { output_buf = (char *) mxCalloc((size_t)strlen(param.amg)+1, (size_t)sizeof(char)); strcpy(output_buf,param.amg); fout = mxCreateString(output_buf); } else if (!strcmp("presmoother",fnames[ifield])) { output_buf = (char *) mxCalloc((size_t)strlen(param.presmoother)+1, (size_t)sizeof(char)); strcpy(output_buf,param.presmoother); fout = mxCreateString(output_buf); } else if (!strcmp("postsmoother",fnames[ifield])) { output_buf = (char *) mxCalloc((size_t)strlen(param.postsmoother)+1, (size_t)sizeof(char)); strcpy(output_buf,param.postsmoother); fout = mxCreateString(output_buf); } else if (!strcmp("typecoarse",fnames[ifield])) { output_buf = (char *) mxCalloc((size_t)strlen(param.typecoarse)+1, (size_t)sizeof(char)); strcpy(output_buf,param.typecoarse); fout = mxCreateString(output_buf); } else if (!strcmp("typetv",fnames[ifield])) { output_buf = (char *) mxCalloc((size_t)strlen(param.typetv)+1, (size_t)sizeof(char)); strcpy(output_buf,param.typetv); fout = mxCreateString(output_buf); } else if (!strcmp("FCpart",fnames[ifield])) { output_buf = (char *) mxCalloc((size_t)strlen(param.FCpart)+1, (size_t)sizeof(char)); strcpy(output_buf,param.FCpart); fout = mxCreateString(output_buf); } else if (!strcmp("solver",fnames[ifield])) { output_buf = (char *) mxCalloc((size_t)strlen(param.solver)+1, (size_t)sizeof(char)); strcpy(output_buf, param.solver); fout = mxCreateString(output_buf); } else if (!strcmp("ordering",fnames[ifield])) { output_buf = (char *) mxCalloc((size_t)strlen(param.ordering)+1, (size_t)sizeof(char)); strcpy(output_buf, param.ordering); fout = mxCreateString(output_buf); } else { /* Allocate memory for input and output strings. */ input_buf = (char *) mxCalloc((size_t)buflen, (size_t)sizeof(char)); output_buf = (char *) mxCalloc((size_t)buflen, (size_t)sizeof(char)); /* Copy the string data from tmp into a C string input_buf. */ status = mxGetString(tmp, input_buf, buflen); sizebuf = buflen*sizeof(char); memcpy(output_buf, input_buf, (size_t)sizebuf); fout = mxCreateString(output_buf); } } else { fout = mxCreateNumericArray((mwSize)ndim, dims, classIDflags[ifield], mxREAL); pdata = mxGetData(fout); sizebuf = mxGetElementSize(tmp); if (!strcmp("elbow",fnames[ifield])) { dbuf=param.elbow; memcpy(pdata, &dbuf, (size_t)sizebuf); } else if (!strcmp("lfilS",fnames[ifield])) { dbuf=param.lfilS; memcpy(pdata, &dbuf, (size_t)sizebuf); } else if (!strcmp("lfil",fnames[ifield])) { dbuf=param.lfil; memcpy(pdata, &dbuf, (size_t)sizebuf); } else if (!strcmp("maxit",fnames[ifield])) { dbuf=param.maxit; memcpy(pdata, &dbuf, (size_t)sizebuf); } else if (!strcmp("droptolS",fnames[ifield])) { dbuf=param.droptolS; memcpy(pdata, &dbuf, (size_t)sizebuf); } else if (!strcmp("droptolc",fnames[ifield])) { dbuf=param.droptolc; memcpy(pdata, &dbuf, (size_t)sizebuf); } else if (!strcmp("droptol",fnames[ifield])) { dbuf=param.droptol; memcpy(pdata, &dbuf, (size_t)sizebuf); } else if (!strcmp("condest",fnames[ifield])) { dbuf=param.condest; memcpy(pdata, &dbuf, (size_t)sizebuf); } else if (!strcmp("restol",fnames[ifield])) { dbuf=param.restol; memcpy(pdata, &dbuf, (size_t)sizebuf); } else if (!strcmp("npresmoothing",fnames[ifield])) { dbuf=param.npresmoothing; memcpy(pdata, &dbuf, (size_t)sizebuf); } else if (!strcmp("npostmoothing",fnames[ifield])) { dbuf=param.npostsmoothing; memcpy(pdata, &dbuf, (size_t)sizebuf); } else if (!strcmp("ncoarse",fnames[ifield])) { dbuf=param.ncoarse; memcpy(pdata, &dbuf, (size_t)sizebuf); } else if (!strcmp("matching",fnames[ifield])) { dbuf=param.matching; memcpy(pdata, &dbuf, (size_t)sizebuf); } else if (!strcmp("nrestart",fnames[ifield])) { dbuf=param.nrestart; memcpy(pdata, &dbuf, (size_t)sizebuf); } else if (!strcmp("damping",fnames[ifield])) { dbuf=param.damping; memcpy(pdata, &dbuf, (size_t)sizebuf); } else if (!strcmp("mixedprecision",fnames[ifield])) { dbuf=param.mixedprecision; memcpy(pdata, &dbuf, (size_t)sizebuf); } else { memcpy(pdata, mxGetData(tmp), (size_t)sizebuf); } } /* Set each field in output structure */ mxSetFieldByNumber(options_output, (mwIndex)0, ifield, fout); } mxFree(fnames); return; }
/* entry point: load input variables and run the algorithm */ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { mxArray *pA; mwSize dimsOutput[2]; void* start_of_pr; mxClassID datatype; int bytes_to_copy; /* ============================================= * Handle input variables. * ============================================= */ /* * input variable 0: tScale */ tScale = (float)mxGetScalar(prhs[0]); /* * input variable 1: rScale */ rScale = (float)mxGetScalar(prhs[1]); /* * input variable 2: cScale */ cScale = (float)mxGetScalar(prhs[2]); /* * input variable 3: rotation */ rotation = (float)mxGetScalar(prhs[3]); /* * input variable 4: inRow */ inRow = (float*)mxGetPr(prhs[4]); nElement = mxGetM(prhs[4]) * mxGetN(prhs[4]); datatype = mxGetClassID(prhs[4]); if (datatype != mxSINGLE_CLASS) mexErrMsgTxt("warning !! single precision required."); /* * input variable 5: inCol */ inCol = (float*)mxGetPr(prhs[5]); datatype = mxGetClassID(prhs[5]); if (datatype != mxSINGLE_CLASS) mexErrMsgTxt("warning !! single precision required."); /* * input variable 6: inO */ inO = (float*)mxGetPr(prhs[6]); datatype = mxGetClassID(prhs[6]); if (datatype != mxSINGLE_CLASS) mexErrMsgTxt("warning !! single precision required."); /* * input variable 7: inS */ inS = (float*)mxGetPr(prhs[7]); datatype = mxGetClassID(prhs[7]); if (datatype != mxSINGLE_CLASS) mexErrMsgTxt("warning !! single precision required."); /* * input variable 8: nOri (number of orientation levels within 0 ~ pi) */ nOri = (float)mxGetScalar(prhs[8]); /* ============================================= * Computation. * ============================================= */ outRow = (float*)mxCalloc( nElement, sizeof(*outRow) ); outCol = (float*)mxCalloc( nElement, sizeof(*outCol) ); outO = (float*)mxCalloc( nElement, sizeof(*outO) ); outS = (float*)mxCalloc( nElement, sizeof(*outS) ); compute(); /* ============================================= * Handle output variables. * ============================================= */ /* * output variable 0: outRow */ dimsOutput[0] = nElement; dimsOutput[1] = 1; pA = mxCreateNumericArray( 2, dimsOutput, mxSINGLE_CLASS, mxREAL ); /* populate the real part of the created array */ start_of_pr = (float*)mxGetData(pA); bytes_to_copy = dimsOutput[0] * dimsOutput[1] * mxGetElementSize(pA); memcpy( start_of_pr, outRow, bytes_to_copy ); plhs[0] = pA; /* * output variable 1: outCol */ dimsOutput[0] = nElement; dimsOutput[1] = 1; pA = mxCreateNumericArray( 2, dimsOutput, mxSINGLE_CLASS, mxREAL ); /* populate the real part of the created array */ start_of_pr = (float*)mxGetData(pA); bytes_to_copy = dimsOutput[0] * dimsOutput[1] * mxGetElementSize(pA); memcpy( start_of_pr, outCol, bytes_to_copy ); plhs[1] = pA; /* * output variable 2: outO */ dimsOutput[0] = nElement; dimsOutput[1] = 1; pA = mxCreateNumericArray( 2, dimsOutput, mxSINGLE_CLASS, mxREAL ); /* populate the real part of the created array */ start_of_pr = (float*)mxGetData(pA); bytes_to_copy = dimsOutput[0] * dimsOutput[1] * mxGetElementSize(pA); memcpy( start_of_pr, outO, bytes_to_copy ); plhs[2] = pA; /* * output variable 3: outS */ dimsOutput[0] = nElement; dimsOutput[1] = 1; pA = mxCreateNumericArray( 2, dimsOutput, mxSINGLE_CLASS, mxREAL ); /* populate the real part of the created array */ start_of_pr = (float*)mxGetData(pA); bytes_to_copy = dimsOutput[0] * dimsOutput[1] * mxGetElementSize(pA); memcpy( start_of_pr, outS, bytes_to_copy ); plhs[3] = pA; }