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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:42:31+00:00 2026-05-19T23:42:31+00:00

I want to create an n-dimensional array of doubles. At compile-time, the number of

  • 0

I want to create an n-dimensional array of doubles. At compile-time, the number of dimensions n is not known.

I ended up defining the array as a dictionary, with the key being an array of ints corresponding to the different axes (so in a 3-dimensional array, I’d supply [5, 2, 3] to get the double at (5, 2, 3) in the array.

However, I also need to populate the dictionary with doubles from (0, 0, … 0) to (m1, m2, … mn), where m1 to mn is the length of each axis.

My initial idea was to create nested for-loops, but as I still don’t know how many I’d need (1 for each dimension), I can’t do this at compile-time.

I hope I’ve formulated the question in an understandable manner, but feel free to ask me to elaborate parts.

  • 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-19T23:42:32+00:00Added an answer on May 19, 2026 at 11:42 pm

    A quick followup on this matter:

    We used the Array.CreateInstance method with success, but as someone predicted, it was fairly inefficient, and additionally created readability problems.

    Instead, we have developed a method, where the n-dimensional array is converted into a 1-dimensional (normal) array.

    public static int NDToOneD(int[] indices, int[] lengths)
    {
      int ID = 0;
      for (int i = 0; i < indices.Length; i++)
      {
        int offset = 1;
        for (int j = 0; j < i; j++)
    {
          offset *= lengths[j];
    }
        ID += indices[i] * offset;
      }
      return ID;
    }
    
    1DtoND(int[] indices, int[] arrayLengths)
    {
      int[] indices = new int[lengths.Length];
      for (int i = lengths.Length - 1; i >= 0; i--)
      {
        int offset = 1;
        for (int j = 0; j < i; j++)
        {
          offset *= lengths[j];
        }
        int remainder = ID % offset;
        indices[i] = (ID - remainder) / offset;
        ID = remainder;
      }
      return indices;
    }
    

    This is essentially a generalisation on the conversion of cartesian coordinates to a single integer and back again.

    Our testing is not formalized, so any speedup we have gained is entirely anecdotal, but for my machine, it has given about a 30-50% speedup, depending on the sample size, and the readability of the code has improved by a wide margin.

    Hope this helps anyone who stumbles upon this question.

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

Sidebar

Related Questions

I want to create a two dimensional array dynamically. I know the number of
I want to create a 2-dimensional array with an index-number in each first element.
I want to create a two-dimensional array in which I want to store records
If I want to create a 2D array, with dimensions specified by user input,
How do I create an array of multiple dimensions? For example, I want an
I want PHP to randomly create a multi-dimensional array by picking a vast amount
I have an array of two dimensional Arrays. I want to create a new
I want to create two dimensional array of buttons in windows form. I have
I was wanting to create a 2 dimensional string array that had dimensions string[5][*]
i want to create a single dimensional array in MATLAB inside for loop, please

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.