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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:06:40+00:00 2026-05-19T12:06:40+00:00

Sure, you can have nested lists to represent multidimensional arrays, but that seems costly…

  • 0

Sure, you can have nested lists to represent multidimensional arrays, but that seems costly…

[[0, 1], [2, 3]]

Is there some way to “encode” and “decode” the coordinate into a single number, and use that number to lookup the corresponding element?

[0, 1, 2, 3]

This needs to work with n-dimensions, not just two, and the best I could come up with for encoding is:

def getcellindex(self, location):
  cindex = 0
  cdrop = self.gridsize # where self.gridsize is the number of cells
  for index in xrange(self.numdimensions): # where self.numdimensions is the number of dimensions
    # where self.dimensions is a tuple of the different sizes of the corresponding dimension
    cdrop /= self.dimensions[index]
    cindex += cdrop * location[index]
  return cindex

There’re probably ways to optimize this, but more importantly, how do I reverse the process? And, does this function work?

  • 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-19T12:06:40+00:00Added an answer on May 19, 2026 at 12:06 pm
    def getlocation(self, cellindex):
        res = []
        for size in reversed(self.dimensions):
            res.append(cellindex % size)
            cellindex /= size
        return res[::-1]
    

    Or, for the full test case

    class ndim:
        def __init__(self):
            self.dimensions=[8,9,10]
            self.numdimensions=3
            self.gridsize=8*9*10
    
        def getcellindex(self, location):
            cindex = 0
            cdrop = self.gridsize
            for index in xrange(self.numdimensions):
                cdrop /= self.dimensions[index]
                cindex += cdrop * location[index]
            return cindex
    
        def getlocation(self, cellindex):
            res = []
            for size in reversed(self.dimensions):
                res.append(cellindex % size)
                cellindex /= size
            return res[::-1]
    
    n=ndim()
    print n.getcellindex((0,0,0))
    print n.getcellindex((0,0,1))
    print n.getcellindex((0,1,0))
    print n.getcellindex((1,0,0))
    
    print n.getlocation(90)
    print n.getlocation(10)
    print n.getlocation(1)
    print n.getlocation(0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that __stdcall functions can't have ellipses, but I want to be sure
I've just installed VS2008 and have run into a problem that I'm sure can
I'm sure this must have been asked before, but I can't find it. Can
I'm pretty sure it's easy but I can't seem to find it. I have
I have some code that runs fairly well, but I would like to make
I have two distinct modules that can be used independently, but Module2 is dependent
I have a link_to_remote and I want to make sure people can only click
I have the following interfaces. I'm not sure how I can use Moq to
I have a situation I'm not sure how to debug. I'm hoping someone can
In Rails 2.x you can use validations to make sure you have a unique

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.