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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:33:43+00:00 2026-06-08T06:33:43+00:00

I have to read a matrix of double, handling its values and insert them

  • 0

I have to read a matrix of double, handling its values and insert them in a new matrix, whose one of its dimension is unkwown at the beginning.

In a static memory allocation, my code is:

#include <mex.h>

void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
  const mxArray *I = prhs[0];
  double *indata = mxGetPr(I);
  double *submtx = mxGetPr(prhs[1]);
  const int *size = mxGetDimensions(I);
  int xp = (int)submtx[0], yp = (int)submtx[1], zp = (int)submtx[2];
  int xi = size[0], yi = size[1], zi = size[2];

  int numsubmtx = (xi - xp + 1)*(yi - yp + 1)*(zi - zp + 1);
  int out_rows = xp*yp*zp;
  int out_cols = numsubmtx;
  mxArray *out = mxCreateDoubleMatrix( out_rows, out_cols, mxREAL );
  double *outdata = mxGetPr(out);

  int submtx_counter = 0;
  for( int z_offset = 0; ...; z_offset++ ){
        for( int y_offset = 0; ...; y_offset++ ){
              for( int x_offset = 0; ...; x_offset++ ){
                    int row = 0;
                    for( int z_counter = 0; ...; z_counter++ ){
                          for( int y_counter = 0; ...; y_counter++ ){
                                for( int x_counter = 0; ...; x_counter++ ){
                                      outdata[submtx_counter*out_rows + row] = 
                                            indata[ (x_offset+x_counter) + (y_offset+y_counter)*xi + (z_offset+z_counter)*xi*yi ];
                                      ++row;
                                }}}
              ++submtx_counter;
              }}}
  plhs[0] = out;
}

In a dynamic version, I do not know the value of out_cols, so I have to reallocate *out when a condition on the values of indata is satisfied.

My idea is something like this:

#include <mex.h>

void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
  const mxArray *I = prhs[0];
  double *indata = mxGetPr(I);
  double *submtx = mxGetPr(prhs[1]);
  const int *size = mxGetDimensions(I);
  int xp = (int)submtx[0], yp = (int)submtx[1], zp = (int)submtx[2];
  int xi = size[0], yi = size[1], zi = size[2];

  int numsubmtx = (xi - xp + 1)*(yi - yp + 1)*(zi - zp + 1);
  int out_rows = xp*yp*zp;
  //int out_cols = numsubmtx; NOW UNKNOWN!
  mxArray *out = NULL;
  double *outdata = mxGetPr(out);

  int submtx_counter = 0;
  for( int z_offset = 0; ...; z_offset++ ){
        for( int y_offset = 0; ...; y_offset++ ){
              for( int x_offset = 0; ...; x_offset++ ){
                    int row = 0;
                    double condition=0;
                    for( int z_counter = 0; ...; z_counter++ ){
                          for( int y_counter = 0; ...; y_counter++ ){
                                for( int x_counter = 0; ...; x_counter++ ){
                                      condition += indata[ (x_offset+x_counter) + (y_offset+y_counter)*xi + (z_offset+z_counter)*xi*yi ]/(xp*yp*zp);
                                      ++row;
                                }}}
                    if(coundition>0.5){
                      out = mxRealloc(out, (submtx_counter+1)*out_rows*sizeof(double));
                      double *outdata = mxGetPr(out);
                      int row = 0;
                      for( int z_counter = 0; ...; z_counter++ ){
                          for( int y_counter = 0; ...; y_counter++ ){
                                for( int x_counter = 0; ...; x_counter++ ){
                                      outdata[submtx_counter*out_rows + row] = indata[ (x_offset+x_counter) + (y_offset+y_counter)*xi + (z_offset+z_counter)*xi*yi ];
                                      ++row;
                                }}}
              ++submtx_counter;
              }
              }}}
  plhs[0] = out;
}

What is wrong?

  • 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-08T06:33:44+00:00Added an answer on June 8, 2026 at 6:33 am

    I haven’t looked closely at the logic of your code, but I can see the following problems:

    mxArray *out = NULL;
    double *outdata = mxGetPr(out);
    

    and

    out = mxRealloc(out, (submtx_counter+1)*out_rows*sizeof(double));
    

    will have strange behavior. An mxArray is a structure, and you want to reallocate memory for the data pointed to by the structure rather than the structure itself. Instead, try

    double *outdata = mxMalloc(out_rows*sizeof(double));
    

    and

    outdata = mxRealloc(outdata, (submtx_counter+1)*out_rows*sizeof(double));
    

    and then at the very end, create your output matrix:

    mxArray *out = mxCreateDoubleMatrix(out_rows, submtx_counter, mxREAL);
    mxSetPr(out, outdata);
    plhs[0] = out;
    

    This will guarantee that the metadata in out gives you a matrix of the correct size, etc.

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

Sidebar

Related Questions

I have a matrix (1051*1051) that has 0zeros along its diagonal and values greater
Given graph, how could i represent it using adj matrix?. I have read lots
How can we display an image's matrix in MATLAB ? I have read the
Hello I have a homework assignment where I need to read two matrix .txt
I have two abstract classes called Robot and GeometricElement . Each one of them
I have a 3000x300 matrix file (float). when I read and convert to float,
I have read many answers to questions about dynamically resizing NSWindows and nothing has
I have read various posts in stackoverflow and I am trying to figure out
I have read the Apple documentation, and many postings on the subject on SO,
I have read pretty much the entire documentation even beyond on the AWS AS

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.