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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:34:21+00:00 2026-06-13T21:34:21+00:00

I have saved multidimensional arrays in Matlab before (e.g. an array A that has

  • 0

I have saved multidimensional arrays in Matlab before (e.g. an array A that has size 100x100x100) using a .mat file and that worked out very nicely.

What is the best way to save such multidimensional arrays in C? The only way I can think of is to store it as a 2D array (e.g. convert a KxNxM array to a KNxM array) and be careful in remembering how it was saved.

What is also desired is to save it in a way that can be opened later in Matlab for post-processing/plotting.

  • 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-13T21:34:22+00:00Added an answer on June 13, 2026 at 9:34 pm

    C does 3D arrays just fine:

    double data[D0][D1][D2];
    ...
    data[i][j][k] = ...;
    

    although for very large arrays such as in your example, you would want to allocate the arrays dynamically instead of declaring them as auto variables such as above, since the space for auto variables (usually the stack, but not always) may be very limited.

    Assuming all your dimensions are known at compile time, you could do something like:

    #include <stdlib.h>
    ...
    #define DO 100
    #define D1 100
    #define D2 100
    ...
    double (*data)[D1][D2] = malloc(sizeof *data * D0);
    if (data)
    {
      ...
      data[i][j][k] = ...;
      ...
      free(data);
    }
    

    This will allocate a D0xD1xD2 array from the heap, and you can access it like any regular 3D array.

    If your dimensions are not known until run time, but you’re working with a C99 compiler or a C2011 compiler that supports variable-length arrays, you can do something like this:

    #include <stdlib.h>
    ...
    size_t d0, d1, d2;
    d0 = ...;
    d1 = ...;
    d2 = ...;
    ...
    double (*data)[d1][d2] = malloc(sizeof *data * d0);
    if (data)
    {
      // same as above
    }
    

    If your dimensions are not known until runtime and you’re working with a compiler that does not support variable-length arrays (C89 or earlier, or a C2011 compiler without VLA support), you’ll need to take a different approach.

    If the memory needs to be allocated contiguously, then you’ll need to do something like the following:

    size_t d0, d1, d2;
    d0 = ...;
    d1 = ...;
    d2 = ...;
    ...
    double *data = malloc(sizeof *data * d0 * d1 * d2);
    if (data)
    {
      ...
      data[i * d0 * d1 + j * d1 + k] = ...;
      ...
      free(data);
    }
    

    Note that you have to map your i, j, and k indices to a single index value.

    If the memory doesn’t need to be contiguous, you can do a piecemeal allocation like so:

    double ***data;
    ...
    data = malloc(d0 * sizeof *data);
    if (data)
    {
      size_t i;
      for (i = 0; i < d0; i++)
      {
        data[i] = malloc(d1 * sizeof *data[i]);
        if (data[i])
        {
          size_t j;
          for (j = 0; j < d1; j++)
          {
            data[i][j] = malloc(d2 * sizeof *data[i][j]);
            if (data[i][j])
            {
              size_t k;
              for (k = 0; k < d2; k++)
              {
                data[i][j][k] = initial_value();
              }
            }
          }
        }
      }
    }
    

    and deallocate it as

    for (i = 0; i < d0; i++)
    {
      for (j = 0; j < d1; j++)
      {
        free(data[i][j]);
      }
      free(data[i]);
    }
    free(data);
    

    This is not recommended practice, btw; even though it allows you to index data as though it were a 3D array, the tradeoff is more complicated code, especially if malloc fails midway through the allocation loop (then you have to back out all the allocations you’ve made so far). It may also incur a performance penalty since the memory isn’t guaranteed to be well-localized.

    EDIT

    As for saving this data in a file, it kind of depends on what you need to do.

    The most portable is to save the data as formatted text, such as:

    #include <stdio.h>
    FILE *dat = fopen("myfile.dat", "w"); // opens new file for writing
    if (dat)
    {
      for (i = 0; i < D0; i++)
      {
        for (j = 0; j < D1; j++)
        {
          for (k = 0; k < D2; k++)
          {
            fprintf(dat, "%f ", data[i][j][k]);
          }
          fprintf(dat, "\n");
        }
        fprintf(dat, "\n");
      }
    }
    

    This writes the data out as a sequence of floating-point numbers, with a newline at the end of each row, and two newlines at the end of each “page”. Reading the data back in is essentially the reverse:

    FILE *dat = fopen("myfile.dat", "r"); // opens file for reading
    if (dat)
    {
      for (i = 0; i < D0; i++)
        for (j = 0; j < D1; j++)
          for (k = 0; k < D2; k++)
            fscanf(dat, "%f", &data[i][j][k]);
    }
    

    Note that both of these snippets assume that the array has a known, fixed size that does not change from run to run. If that is not the case, you will obviously have to store additional data in the file to determine how big the array needs to be. There’s also nothing resembling error handling.

    I’m leaving a lot of stuff out, since I’m not sure what your goal is.

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

Sidebar

Related Questions

I have a bunch of multidimensional arrays serialized and saved in one txt file,
I have a kmz file that I have saved as kml to use in
I have a multidimensional array or arrays which I also use in my configuration
i have saved the pdf file to the database using file upload . now
I have a very large multidimensional vector that changes in size all the time.
I have saved my view file in netbeans IDE as .phtml. File has both
I have saved an html page using the following line of code : NSURL
I have saved a pdf file in the apps documents folder on the iPad.
I have saved data which was saved using NSUserDefaults. I was under the impression
I have saved a dataset in the sql database in an xml column using

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.