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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:13:23+00:00 2026-06-03T13:13:23+00:00

When I have an array (of 1D, 2D, 3D, or more dimensions), I would

  • 0

When I have an array (of 1D, 2D, 3D, or more dimensions), I would like to be able to translate an index (from this flatten array) into its coordinates.

For instance, considering * square:

3D array (of [2,3,2] shape)

arr = [ [ [ nil, nil ],
          [ nil, nil ],
          [ nil, nil ] ],
        [ [ "*", nil ],
          [ nil, nil ],
          [ nil, nil ] ] ]

arr.flatten[6]           # => "*"
arr.index2coordinates(6) # => [1,0,0] or [1][0][0]

4D array (of [2,3,1,2] shape)

arr = [ [ [ [ nil, nil ],
            [ nil, "*" ],
            [ nil, nil ] ] ],
        [ [ [ nil, nil ],
            [ nil, nil ],
            [ nil, nil ] ] ] ]

arr.flatten[3]           # => "*"
arr.index2coordinates(3) # => [0,0,1,1] or [0][0][1][1]

1D array (of [5] shape)

arr = [ nil, nil, nil, "*", nil ]

arr.flatten[3]           # => "*"
arr.index2coordinates(3) # => [3]

How can we do a such Array#index2coordinates method? In a sense, this question is the inverse of Convert vector to integer question. Thanks a lot.

  • 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-03T13:13:26+00:00Added an answer on June 3, 2026 at 1:13 pm

    Examples, step by step:

    # 3D array (of [2,3,2] shape)
    arr.flatten[6]           # => "*"
    arr.index2coordinates(6) # => [1,0,0] or [1][0][0]
    
    6 % 2              # => 0
    
    (6 / 2) % 3        # (3) % 3 => 0
    
    ((6 / 2) / 3) % 2  # ((3) / 3) %2 => 1 % 2 => 1
    
    
    # 4D array (of [2,1,3,2] shape)    
    arr.flatten[3]           # => "*"
    arr.index2coordinates(3) # => [0,0,1,1] or [0][0][1][1]
    
    3 % 2                     # => 1
    
    (3 / 2) % 3               # => 1 % 3 => 1
    
    ((3 / 2) / 3) % 1         # => 1 % 1 => 0
    
    (((3 / 2) / 3) / 1) % 2   # 0 % 2 => 0
    

    Simple code for second example

    result = []
    index = 3
    
    [2,1,3,2].reverse.each do |dimension|
      result = [index % dimension] + result
      index = (index / dimension)
    end
    
    result          # => [0, 0, 1, 1]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a factorised array of n dimensions and I would like to develop
I have the following 2 arrays and would like to combine them. I'm more
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
I have array result like this: Array ( [0] => stdClass Object ( [id_global_info]
I have a table array which looks like this: tablearray = [ {'column1': 1,
I have a array with a variable amount of values. Is there a more
An array is defined of assumed elements like I have array like String[] strArray
Say I have a collection of object arrays of equal dimension, like this: var
I have an 2 dimensional array. Each of the row vectors, in this case,
I am new to Fortran, and I would like to be able to write

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.