Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6076113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:34:14+00:00 2026-05-23T10:34:14+00:00

I have a working Matlab C code (mex files) which currently uses double precision.

  • 0

I have a working Matlab C code (mex files) which currently uses double precision. Thus I replaced

double *datOut = mxGetPr(mxOut) by float *datOut = (float*)mxGetData(mxOut);,

mxCreateDoubleMatrix by mxCreateNumericArray()

and

the datatypes of the variables double by float. The only other mex-Function which is being used is mxDuplicateArray() but nothing else. I didn’t change anything to this call… Now I have a Code running which never finishes. I stripped it down quite a bit so that I hope it’s short enough that somebody could help me:

float myFunc(const mxArray *point, int index)
{
    float *dat = (float*)mxGetData(point);
    return dat[index]*dat[index]*dat[index];
}

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
    float h, f0, df1, df2, diff;

    // Input Vars #1
    float diff  = (float)mxGetScalar(prhs[1]);
    float H = (float)mxGetScalar(prhs[2]);
    int index1 = (int)mxGetScalar(prhs[3]);
    int index2 = (int)mxGetScalar(prhs[4]);

    // Input Vars #2 -> Duplicate it
    mxArray *newPnt = mxDuplicateArray(prhs[0]);
    float *newPntDat = (float*)mxGetData(newPnt);

    // ...
    // PERHAPS SOME UNIMPORANT CODE HERE ...
    // ...
    h = H;
    f0 = myFunc(prhs[0], index1);

    newPntData[ index2 ] += h;
    df1 = (myFunc(newPnt, index1)-f0)/h;
    while(true)
    {
        h /= 2;

        newPntDat[ index2 ] -= h;
        df2 = (myFunc(newPnt, index1)-f0)/h;

        // If precision is okay
        if(abs(df2-df1) <= diff)
            break;

        // Save for next loop iteration
        df1 = df2;
    }

    // Return df2-Value to Matlab
}

somehow it’s an infinite loop and I dunno why since the precision which is defined via diff should be easily reachable for the given function myFunc(). The identicall code runs fine when using double precision with the both functions double *datOut = mxGetPr(mxOut) and mxCreateDoubleMatrix. I also tried to call the mex-Function by explicitly pass the point via point = zeros(rows, 1, 'single');.

Thanks very much for pointing me to the right direction or giving me ANY hint about it. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:34:15+00:00Added an answer on May 23, 2026 at 10:34 am

    You need to replace abs() with fabs().

    In general, in such cases, I would use mexPrintf() to print the values that affect the termination condition. I.e., if the above change does not help, try adding

    mexPrintf("%g %g %g %g\n",df2,df1,diff, fabs(df2-df1));
    

    Just to make sure the behavior is as you expected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on some legacy C++ code that uses variable length structures
I am working on some fourier transform code in matlab, and have come across
I have some MATLAB code I've been working with. There are three sections, say
I have been working lately on a number of iterative algorithms in MATLAB, and
I'm working on my MATLAB code in a number of different locations, and it
I have a line in the code I'm working on like this: feval('func_name', myArg);
I'm working on research model which outputs results to matlab's .mat file format, and
I'm currently working on writing a version of the MATLAB RegionProps function for GNU
I have working code but it seems a bit strange that I have to
I am currently working on my fourth year project (computer science) which involves the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.