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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:35:24+00:00 2026-06-15T03:35:24+00:00

I have two vectors of arbitrary and equal length a <- c(0.8,0.8,0.8) b <-

  • 0

I have two vectors of arbitrary and equal length

a <- c(0.8,0.8,0.8) 
b <- c(0.4,0.4,0.4)
n <- length(a)

From these I need to assemble an 2n by 2n matrix of the form:

x = [1-a1  b1    1-a2  b2    1-a3  b3
     a1    1-b1  a2    1-b2  a3    1-b3
     1-a1  b1    1-a2  b2    1-a3  b3
     a1    1-b1  a2    1-b2  a3    1-b3
     1-a1  b1    1-a2  b2    1-a3  b3
     a1    1-b1  a2    1-b2  a3    1-b3]

I currently do this using

x <- matrix(rep(as.vector(rbind(
                          c(1-a,a), 
                          c(b, 1-b))),
                n),
            ncol=n*2, byrow=TRUE)

How can I speed up this operation? Profiling indicates that matrix is taking the most time:

Rprof("out.prof")
for (i in 1:100000) {

x <- matrix(rep(as.vector(rbind(
  c(1-a,a), 
  c(b, 1-b))),
                n),
            ncol=n*2, byrow=TRUE)

}
Rprof(NULL)
summaryRprof("out.prof")

##$by.self
##            self.time self.pct total.time total.pct
##"matrix"         1.02    63.75       1.60    100.00
##"rbind"          0.24    15.00       0.36     22.50
##"as.vector"      0.18    11.25       0.54     33.75
##"c"              0.10     6.25       0.10      6.25
##"*"              0.04     2.50       0.04      2.50
##"-"              0.02     1.25       0.02      1.25
##
##$by.total
##            total.time total.pct self.time self.pct
##"matrix"          1.60    100.00      1.02    63.75
##"as.vector"       0.54     33.75      0.18    11.25
##"rbind"           0.36     22.50      0.24    15.00
##"c"               0.10      6.25      0.10     6.25
##"*"               0.04      2.50      0.04     2.50
##"-"               0.02      1.25      0.02     1.25
##
##$sample.interval
##[1] 0.02
##
##$sampling.time
##[1] 1.6
  • 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-15T03:35:25+00:00Added an answer on June 15, 2026 at 3:35 am

    I don’t think there is an alternative to matrix being the slowest part of your profile, but you can definitely save a little time by optimizing the rest. For example:

    x <- matrix(rbind(c(1-a,a), c(b, 1-b)), 2*n, 2*n, byrow=TRUE)
    

    Also, although I would not recommend it, you can save a little extra time by using the Internal matrix function:

    x <- .Internal(matrix(rbind(c(1-a,a), c(b, 1-b)),
                          n*2, n*2, TRUE, NULL, FALSE, FALSE))
    

    Here are some benchmarks:

    benchmark(
       method0 = matrix(rep(as.vector(rbind(c(1-a,a), c(b, 1-b))), n),
                        ncol=n*2, byrow=TRUE),
       method1 = matrix(rbind(c(1-a,a), c(b, 1-b)), 2*n, 2*n, byrow=TRUE),
       method2 = .Internal(matrix(rbind(c(1-a,a), c(b, 1-b)),
                                  n*2, n*2, TRUE, NULL, FALSE, FALSE)),
       replications = 100000,
       order = "relative")
    
    #      test replications elapsed relative user.self sys.self user.child sys.child
    # 3 method2       100000    1.00     1.00      0.99        0         NA        NA
    # 2 method1       100000    1.13     1.13      1.12        0         NA        NA
    # 1 method0       100000    1.46     1.46      1.46        0         NA        NA
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two vectors o and c of equal length: o = [-1 -1
I have two vectors of values and one vector of weights, and I need
Currently I have two larger vectors of 50+ strings I want to be able
I have two vectors of matching lengths. They are readings from two different sensors
I have two vectors: sensorA of length 927 and sensorB of length 1250. I
Suppose now I have two vectors of same length: A = [1 2 2
I have two vectors and want to compare their contents (strings) but this does
I have two vectors in the form of columns, e.g.: a = 1 2
I have two vectors. vector<Object> objects; vector<string> names; These two vectors are populated and
If I have two vectors of the same length A<-c(5,10) and B<-c(7,13) 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.