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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:47:23+00:00 2026-05-30T19:47:23+00:00

I have written the code below to generate a matrix containing what is, to

  • 0

I have written the code below to generate a matrix containing what is, to me, a fairly complex pattern. In this case I determined that there are 136 rows in the finished matrix by trial and error.

I could write a function to calculate the number of matrix rows in advance, but the function would be a little complex. In this example the number of rows in the matrix = ((4 * 3 + 1) + (3 * 3 + 1) + (2 * 3 + 1) + (1 * 3 + 1)) * 4.

Is there an easy and efficient way to create matrices in R without hard-wiring the number of rows in the matrix statement? In other words, is there an easy way to let R simply add a row to a matrix as needed when using for-loops?

I have presented one solution that employs rbind at each pass through the loops, but that seems a little convoluted and I was wondering if there might be a much easier solution.

Sorry if this question is redundant with an earlier question. I could not locate a similar question using the search feature on this site or using an internet search engine today, although I think I have found a similar question somewhere in the past.

Below are 2 sets of example code, one using rbind and the other where I used trial and error to set nrow=136 in advance.

Thanks for any suggestions.

v1     <- 5
v2     <- 2
v3     <- 2
v4     <- (v1-1)

my.matrix <- matrix(0, nrow=136, ncol=(v1+4) )

i = 1

for(a in 1:v2) {
  for(b in 1:v3) {
    for(c in 1:v4) {
      for(d in (c+1):v1) {

        if(d == (c+1)) l.s = 4 
        else           l.s = 3

        for(e in 1:l.s) {

          my.matrix[i,c] = 1

            if(d == (c+1)) my.matrix[i,d]  = (e-1)
            else           my.matrix[i,d]  =  e

          my.matrix[i,(v1+1)] = a
          my.matrix[i,(v1+2)] = b
          my.matrix[i,(v1+3)] = c
          my.matrix[i,(v1+4)] = d

          i <- i + 1

        }
      }
    }
  }
}

my.matrix2 <- matrix(0, nrow=1, ncol=(v1+4) )
my.matrix3 <- matrix(0, nrow=1, ncol=(v1+4) )

i = 1

for(a in 1:v2) {
  for(b in 1:v3) {
    for(c in 1:v4) {
      for(d in (c+1):v1) {

        if(d == (c+1)) l.s = 4 
        else           l.s = 3

        for(e  in 1:l.s) {

          my.matrix2[1,c] = 1

          if(d == (c+1)) my.matrix2[1,d]  = (e-1)
          else           my.matrix2[1,d]  =  e

          my.matrix2[1,(v1+1)] = a
          my.matrix2[1,(v1+2)] = b
          my.matrix2[1,(v1+3)] = c
          my.matrix2[1,(v1+4)] = d

          i <- i+1

          if(i == 2) my.matrix3 <- my.matrix2
          else       my.matrix3 <- rbind(my.matrix3, my.matrix2)

          my.matrix2 <- matrix(0, nrow=1, ncol=(v1+4) )

        }
      }
    }
  }
}

all.equal(my.matrix, my.matrix3)
  • 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-30T19:47:25+00:00Added an answer on May 30, 2026 at 7:47 pm

    If you have some upper bound on the size of the matrix,
    you can create a matrix
    large enough to hold all the data

    my.matrix <- matrix(0, nrow=v1*v2*v3*v4*4, ncol=(v1+4) )
    

    and truncate it at the end.

    my.matrix <- my.matrix[1:(i-1),]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a very simple WCF service, that worked fine (code below), then
I have written code that automatically creates CSS sprites based on the IMG tags
I have written code that opens 16 figures at once. Currently, they all open
I have written this code to join ArrayList elements: Can it be optimized more?
I have written some code to ensure that items on an order are all
I was overlooking some code that I had written to generate an A-Z navigation
I have written an autogenerating sitemap code below, it is called every 12 hours.
I have written some C++ code that generates a std::vector. I also have a
I have an embeddeble code of a slide like below. this whole html is
I am have written the following code below to encode a bitarray into custom

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.