Thread: Arrays, pointers, trouble..

  1. #1
    Registered User
    Join Date
    Aug 2010

    Arrays, pointers, trouble..

    Hi all!

    I am trying to write a Matlab mex-file, and I find the c-coding part a bit challenging. I can get the code below to compile, but it doesn't do what is expected:

    #include <math.h>
    #include "mex.h"
    /*Subroutine to calculate the Fuzzy Sample Entropy*/
    void FSE(double *cop, int M, double r, double c, mwSize N, double *FuzzSampEnt) {
        mwIndex m, i, ii, j, jj, k, l;
        double expo, d, *seg_i, *seg_j, count, sumCim, *Cim, CIM[2];
        expo=(log(log(pow(2, c)))/log(r));    
        for (m=M;m<M+2;m++) {
            seg_i=mxCalloc(m, sizeof(double));
            seg_j=mxCalloc(m, sizeof(double));
            Cim=mxCalloc(N-m, sizeof(double));
            for (i=0; i<N-m; i++) {
                for (ii=0; ii<m; ii++){
                for (j=0; j<N-m; j++) {
                    for (jj=0; jj<m; jj++) {
                    /*Finding the maximum absolut value of seg_i-seg_j*/
                    for (k=0; k<m; k++) {
                        if (d < fabs(seg_i[k]-seg_j[k])) {
                    /*if d=0, self match*/
                    if (d>0) {
                        count+=exp(-(pow(d, expo/c)));
            if (m==M){
            else if (m==M+1){
        *FuzzSampEnt=seg_j[k];//-log(CIM[1]/ CIM[0]);
    /*the gateway function*/
    void mexFunction(int nlhs, mxArray *plhs[ ],
            int nrhs, const mxArray *prhs[ ]) {
        int M;
        double *cop, *FuzzSampEnt;
        double c, r;
        mwSize N;
        if(nrhs!=4) {
            mexErrMsgTxt("Four inputs required (cop,M,r,c).");
        /*Assign pointers*/
        plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
        FuzzSampEnt = mxGetPr(plhs[0]);
        /*Call the subroutine */
        FSE(cop, M, r, c, N, FuzzSampEnt);
    The trouble seems to be here: seg_i[ii]=cop[i+ii]; cop[i+ii] seems to be what it should be, but I can't seem to get it's value to seg_i[ii]. Also I'm not sure if it is necessary to use pointers here at all. I know that seg_i and seg_j are either M or M+1 in length and Cim is either N-M or N-M-1 in length.

    The idea would be to make the code as fast as possible, as fast as possible.. So how would you deal with the seg_i, seg_j and Cim? Can you see what I'm doing wrong in here?

    Thank You!


  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Quote Originally Posted by Aino
    The trouble seems to be here: seg_i[ii]=cop[i+ii]; cop[i+ii] seems to be what it should be, but I can't seem to get it's value to seg_i[ii].
    What exactly do you mean by this?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2010
    Well, probably the real problem is earlier than in this point, but until this point I get the stuff out what I want. I have changed the ending of the code, the output parameter FuzzSampEnt to see what it gives out at what point (I'm sure there is a better way). So it would seem that for example FuzzSampEnt=expo; or FuzzSampEnt=cop[ii+i]; give out what they are supposed to, but *FuzzSampEnt=seg_i[k], gives out something funny like -9*10^303. The true output, -log(CIM[1]/ CIM[0]), is commented, but using it the function gives out NaN.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures, arrays, pointers, malloc.
    By omnificient in forum C Programming
    Replies: 9
    Last Post: 02-29-2008, 12:05 PM
  2. vector of arrays of pointers to structures
    By Marksman in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 04:44 AM
  3. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 10:04 AM
  5. Hello all and pointers to multi-dimensional arrays
    By Mario in forum C++ Programming
    Replies: 16
    Last Post: 05-17-2002, 09:05 AM
Website Security Test