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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:15:07+00:00 2026-06-02T23:15:07+00:00

Knowing the multidimensional-array’s shape of a vector, how can we convert it into a

  • 0

Knowing the multidimensional-array’s shape of a vector, how can we convert it into a new vector of one dimension (by flatten the multidimensional-array)?

For example considering the following array:

arr = [
  [
    [ nil, nil ],
    [ nil, nil ],
    [ nil, nil ]
  ],
  [
    [ nil, nil ],
    [ nil, nil ],
    [ nil, nil ]
  ]
]

arr[0][0][0] = "A"
arr[1][0][1] = "B"

arr # =>
[
  [
    [ "A", nil ],
    [ nil, nil ],
    [ nil, nil ]
  ],
  [
    [ nil, "B" ],
    [ nil, nil ],
    [ nil, nil ]
  ]
]

…where A is the origin and B is the destination of the vector. Can write:

shape  = [2, 3, 2]
vector = [1, 0, 1]

From now, supposing we flatten arr, how could we translate the vector? In other words, how to translate this vector of 3 dimensions into a new one of 1 dimension?

This is a special case, because the origin of the vector is also the first coordinate of the array. So we can found the result with:

arr.flatten.index("B") # => 7

Here’s a another example with a 2D-array:

arr = [
  [ "A", nil ],
  [ "B", nil ],
  [ nil, nil ],
  [ nil, nil ],
  [ nil, nil ]
]

We can write this:

shape  = [2, 5]
vector = [1, 0]

And, once again,

arr.flatten.index("B") # => 2

But here is a more complicated example, with a negative vector:

arr = [
  [ "B", nil ],
  [ "A", nil ],
  [ nil, nil ],
  [ nil, nil ],
  [ nil, nil ]
]

shape  = [2, 5]
vector = [-1, 0]

How can the following method can be written ?

vector2index(shape, vector) # => -2

An example (simple) with a 1D-array:

arr = [ nil, "B", nil, nil, "A", nil, nil ]

shape  = [7]
vector = [-3]
vector2index(shape, vector) # => -3

Is there a simple way to flat a vector from an array of any dimensions? Thanks.

  • 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-02T23:15:08+00:00Added an answer on June 2, 2026 at 11:15 pm

    First of all, assuming that first element of array is for X axis, second – for Y axis, third – for Z axis, you have a mistake in second and third example. Third example should be

    shape  = [2,5]
    vector = [0,-1]
    vector2index(shape, vector) # => -2
    

    And if first element of array is for Y axis, second – for X axis, then 2-nd and 3-rd examples is correct, but the first example is wrong.

    If I understand the idea correctly, we need in first example multiply vector[1] to shape[0], multiply vector[2] to shape[0]*shape[1] and then calculate sum of 3 elements. Generally, we don’t need to multiply 0-th element, and we need to multiply n-th element to shape[0]*shape[1]*...*shape[n-1].

    You can implement it this way:

    vector.each_with_index.map { 
      |v, i| i == 0? v: v * shape[0..i-1].inject(:*) 
    }.inject(:+)
    

    Upd. After you updated your question, it becomes more clear. If you want to preserve Ruby’s indexing order, you need to reverse both arrays shape and vector.

    vector.reverse.each_with_index.map { 
      |v, i| i == 0? v: v * shape[0..i-1].reverse.inject(:*) 
    }.inject(:+)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am interested in knowing if its even possible. One example is the variable
knowing what PowerShell can do on a command line I have never learned how
I am not knowing how to use the classes written in netbeans for one
Can any body help me in knowing how to pass a parameter from a
Is there any neat solution of knowing when a thread has been put into
I was just trying this knowing that my select would return one row. Is
Knowing that I can't use HTMLAgilityPack, only straight .NET, say I have a string
Without knowing the keys of a JavaScript Object , how can I turn something
I am interested in knowing whether I can expect the observing object's method to
Knowing that anyone can see my AJAX URL string and or forms how can

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.