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 8715625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:53:30+00:00 2026-06-13T05:53:30+00:00

I have some Matlab code that is run many millions of times as mentioned

  • 0

I have some Matlab code that is run many millions of times as mentioned in this question: Matlab: Does calling the same mex function repeatedly from a loop incur too much overhead?

I’m trying to mex-ify it to see whether that helps. Now, when I generate code from the Matlab code using Matlab Coder tool, the code is generally reasonable, but this one line of Matlab code (in a C++ comment in the first line below) begets this monstrosity, and I have no idea why. Any help in understanding and reducing its complexity would be appreciated.

For context, d is a two dimensional matrix, and s1 is a row vector. s1_idx is assigned to be length(s1) + 1 in preceding C++ code,

/* d(:, 1) = 0:length(s1); */
cdiff = s1_idx_0 - 1;
for (nm1d2 = 0; nm1d2 <= cdiff; nm1d2++) {
   tmp_data[nm1d2] = nm1d2;
}
ndbl = (int32_T)muDoubleScalarFloor((real_T)s1_sizes[1] + 0.5);
apnd = ndbl;
cdiff = ndbl - s1_sizes[1];
if (muDoubleScalarAbs((real_T)cdiff) < 4.4408920985006262E-16 * (real_T)s1_sizes[1]) {
   ndbl++;
   apnd = s1_sizes[1];
} else if (cdiff > 0) {
   apnd = ndbl - 1;
} else {
   ndbl++;
}
if (ndbl > 0) {
   b_tmp_data[0] = 0.0;
   if (ndbl > 1) {
       b_tmp_data[ndbl - 1] = (real_T)apnd;
       nm1 = ndbl - 1;
       nm1d2 = nm1;
       nm1d2 = (int32_T)((uint32_T)nm1d2 >> 1);
       for (cdiff = 1; cdiff <= nm1d2 - 1; cdiff++) {
           b_tmp_data[cdiff] = (real_T)cdiff;
           b_tmp_data[(ndbl - cdiff) - 1] = (real_T)(apnd - cdiff);
       }
       if (nm1d2 << 1 == nm1) {
           b_tmp_data[nm1d2] = (real_T)apnd / 2.0;
       } else {
           b_tmp_data[nm1d2] = (real_T)nm1d2;
           b_tmp_data[nm1d2 + 1] = (real_T)(apnd - nm1d2);
       }
   }
}
cdiff = s1_idx_0 - 1;
for (nm1d2 = 0; nm1d2 <= cdiff; nm1d2++) {
   SD->f0.d_data[tmp_data[nm1d2]] = b_tmp_data[nm1d2];
}
  • 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-06-13T05:53:32+00:00Added an answer on June 13, 2026 at 5:53 am

    That’s pretty funny generated code for what you actually want to accomplish. You just want to stuff the integers 0 to k into an array. But the code generator is built to deal with the general case. So there are three parts to the generated code:

    1. Create an array of indices specifying where, on the left-hand side, the elements from the right-hand side will go. You used the : expression, but you could have used something else. The code generator has to be prepared for you doing something like d(length(s1):0, 1)=0:length(s1).
    2. Create an array of values for the right-hand side. Your just doing sequential integers, but the code generator is prepared to deal with doubles and when creating values from a range of doubles you might have funny rounding issues. It’s checking for all kinds of edge cases.
    3. Finally, there’s the loop that actually assigns the values on the right-hand side to memory slots on the left-hand side as indexed by the array created in step 1.

    In the end, it might be that all you need is:

    cdiff = s1_idx_0 - 1;
    for (nm1d2 = 0; nm1d2 <= cdiff; nm1d2++) {
       SD->f0.d_data[nm1d2] = nm1d2;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some programs written in Matlab that I need to run several times
I have some code that plots triangles in MATLAB. I need to be able
I have some working code that does amplitude modulation and plots it. However I'm
So Currently I have a code that is producing some variables in the Matlab
I have written some matlab code for image analysis that searches for clusters in
I have some MATLAB code and some Java code that need to talk with
I have been given some matlab code compiled using the .net compiler. I can
I have some data generated in MATLAB that I want to process using Perl.
I have some code that will change the background color of a specific label
I have some Matlab code and a GUI for it and I want to

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.