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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:55:15+00:00 2026-06-05T03:55:15+00:00

I have a multi-dimensional array, and the number of its dimensions are unknown until

  • 0

I have a multi-dimensional array, and the number of its dimensions are unknown until the runtime, for example:

dims <- rep(3, dim_count)
arr <- array(0, dims)

Now, having the dims vector I would like to iterate by all the indexes of the array, for example, having

dims <- c(2,3)

I would like to be able to get a series of vectors:

c(1,1)
c(1,2)
c(1,3)
c(2,1)
c(2,2)
c(2,3)

Or just a function generating the next one from the previous one.

The only three ways I could think of were:

  1. iterate over 1:length(arr) and translate those numbers to the
    index vectors – in the above example I would be looking for a (preferably built-in) function doing indexesOf(arr, 4) -> c(2,1).

  2. Get the last generated index vector, increment the element on its last position, and make sure it is in the bounds given by dims.

  3. Generate a matrix, the columns of which would contain all the indexes I need.

But, sadly, neither of the first two approaches is fast or elegant. The third one looks like a decent idea, I can do it on paper, but i can’t code it in R using rbind and so on.

Is there a good way of doing this, preferably without nested loops?

Just for comparison, my ugly looped implementation of #3:

getAllIndexes = function(dims) {
  dimCount <- length(dims)
  ret <- array(1:dims[1], c(1,dims[1]))
  for(i in 2:length(dims)){
    curdims <- dims[i]

    a <- array(rep(ret, curdims), c(nrow(ret), curdims * ncol(ret) ))
    b <- rep(1:curdims, each=ncol(ret))
    ret <- rbind(a, b, deparse.level=0)
  }
  ret
}
  • 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-05T03:55:17+00:00Added an answer on June 5, 2026 at 3:55 am

    If I’m interpreting your request correctly then

    as.matrix(do.call(expand.grid,lapply(dim(arr),seq)))
    

    appears to do #3 …

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

Sidebar

Related Questions

I have a multi-dimensional array similar to the example below that I want to
I have a multi-dimensional array of string . I am willing to convert it
I have a multi-dimensional array, which basically consists of one sub-array for each year.
I have a multi-dimensional array, no problem. How do I interrogator one of the
I have a multi dimensional array. The only actual values (other than other arrays)
I have an multi-dimensional array that I want to send to a PHP script
I have a multi dimensional array, like this: array('name' => array('title'=>'Title','date'=>'Created')) I store it
I have a multi dimensional array in PHP. $f = array('one' => array(*doesntmatter*), two
If I have a multi dimensional array in PHP like so... [0] => Array
Possible Duplicate: php multi-dimensional array remove duplicate I have an array like this: $a

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.