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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:22:45+00:00 2026-06-05T07:22:45+00:00

I set up a 3 dimensinoal matrix of size 365x7x4. x <- array(rep(1, 365*5*4),

  • 0

I set up a 3 dimensinoal matrix of size 365x7x4.

x <- array(rep(1, 365*5*4), dim=c(365, 5, 4))

Now I would to use a for loop to fill each element with a value.
Lets say the value of each element should be sum of row, column and depth.
I guess this is relatively easy.

Thanks! best, F

  • 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-05T07:22:46+00:00Added an answer on June 5, 2026 at 7:22 am

    Using a simpler example so we can see what is being done

    arr <- array(seq_len(3*3*3), dim = rep(3,3,3))
    

    the following code gives the requested output:

    dims <- dim(arr)
    ind <- expand.grid(lapply(dims, seq_len))
    arr[] <- rowSums(ind)
    

    The above gives

    > arr
    , , 1
    
         [,1] [,2] [,3]
    [1,]    3    4    5
    [2,]    4    5    6
    [3,]    5    6    7
    
    , , 2
    
         [,1] [,2] [,3]
    [1,]    4    5    6
    [2,]    5    6    7
    [3,]    6    7    8
    
    , , 3
    
         [,1] [,2] [,3]
    [1,]    5    6    7
    [2,]    6    7    8
    [3,]    7    8    9
    
    > arr[1,1,1]
    [1] 3
    > arr[1,2,3]
    [1] 6
    > arr[3,3,3]
    [1] 9
    

    Update: Using the example in @TimP’s Answer here I update the Answer to show how it can be done in a more R-like fashion.

    Given

    arr <- array(seq_len(3*3*3), dim = rep(3,3,3))
    

    Replace elements of arr with i + j + k unless k > 2, in which case j*k-i is used instead.

    dims <- dim(arr)
    ind <- expand.grid(lapply(dims, seq_len))
    ## which k > 2
    want <- ind[,3] > 2
    arr[!want] <- rowSums(ind[!want, ])
    arr[want] <- ind[want, 2] * ind[want, 3] - ind[want, 1]
    

    Whilst it is tempting to stick with familiar idioms like looping, and contrary to popular belief loops are not inefficient in R, learning to think in a vectorised way will pay off many times over as you learn the language and start applying it to data analysis task.

    Here are some timings on Fabian’s example:

    > x <- array(rep(1, 365*5*4), dim=c(365, 5, 4))
    > system.time({
    + for (i in seq_len(dim(x)[1])) {
    +     for (j in seq_len(dim(x)[2])) {
    +         for (k in seq_len(dim(x)[3])) {
    +             val = i+j+k
    +             if (k > 2) {
    +                 val = j*k-i
    +             }
    +             x[i,j,k] = val
    +         }
    +     }
    + }
    + })
       user  system elapsed 
      0.043   0.000   0.044 
    > arr <- array(rep(1, 365*5*4), dim=c(365, 5, 4))
    > system.time({
    + dims <- dim(arr)
    + ind <- expand.grid(lapply(dims, seq_len))
    + ## which k > 2
    + want <- ind[,3] > 2
    + arr[!want] <- rowSums(ind[!want, ])
    + arr[want] <- ind[want, 2] * ind[want, 3] - ind[want, 1]
    + })
       user  system elapsed 
      0.005   0.000   0.006
    

    and for a much larger (for my ickle laptop at least!) problem

    > x <- array(rep(1, 200*200*200), dim=c(200, 200, 200))
    > system.time({
    + for (i in seq_len(dim(x)[1])) {
    +     for (j in seq_len(dim(x)[2])) {
    +         for (k in seq_len(dim(x)[3])) {
    +             val = i+j+k
    +             if (k > 2) {
    +                 val = j*k-i
    +             }
    +             x[i,j,k] = val
    +         }
    +     }
    + }
    + })
       user  system elapsed 
     51.759   0.129  53.090
    > arr <- array(rep(1, 200*200*200), dim=c(200, 200, 200))
    > system.time({
    +     dims <- dim(arr)
    +     ind <- expand.grid(lapply(dims, seq_len))
    +     ## which k > 2
    +     want <- ind[,3] > 2
    +     arr[!want] <- rowSums(ind[!want, ])
    +     arr[want] <- ind[want, 2] * ind[want, 3] - ind[want, 1]
    + })
       user  system elapsed 
      2.282   1.036   3.397 
    

    but even that may be modest to small by today’s standards. You can see that the looping starts to become ever more uncompetitive because of the all the function calls required by that method.

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

Sidebar

Related Questions

I need to loop through a set of 2-dimensional array of hidden input fields
In my app, the main set of data is a two-dimensional char array (
I have an array to store a set of coordinates for painting a piece
I have a four dimensional array for which I have a static set of
How do I use Cakephp core utility - Set to change the format of
I was trying to group a set of INPUT-elements in a Javascript array and
I have a 2-dimensional integer array (say 1000 by 1000), let's call it matrix.
I have an two-dimensional array number2 . I set it equal to number2copy with
I have a one-dimensional data set in PHP, to be precise, an array with
What would be a quick and simple way to create a two-dimensional array with

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.