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

  • Home
  • SEARCH
  • 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 8574581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:30:22+00:00 2026-06-11T19:30:22+00:00

I have a large matrix that I would like to center: X <- matrix(sample(1:10,

  • 0

I have a large matrix that I would like to center:

X <- matrix(sample(1:10, 5e+08, replace=TRUE), ncol=10000)

Finding the the means is quick and efficient with colMeans:

means <- colMeans(X)

But what’s a good (fast and memory efficient) way to subtract the respective mean from each column? This works, but it doesn’t feel right:

for (i in 1:length(means)){
  X[,i] <- X[,i]-means[i] 
}

Is there a better way?

/edit: Here’s a modification the the various benchmarks DWin wrote, on a larger matrix, including the other posted suggestions:

require(rbenchmark)
X <- matrix(sample(1:10, 5e+07, replace=TRUE), ncol=10000)
frlp.c <- compiler:::cmpfun(function(mat){
  means <- colMeans(mat)
  for (i in 1:length(means)){
    mat[,i] <- mat[,i]-means[i] 
  }
  return(mat)
})

mat.c <- compiler:::cmpfun(function(mat){
  t(t(X) - colMeans(X))
})

swp.c <- compiler:::cmpfun(function(mat){
  sweep(mat, 2, colMeans(mat), FUN='-')
})

scl.c <- compiler:::cmpfun(function(mat){
  scale(mat, scale=FALSE)
})

matmult.c <- compiler:::cmpfun(function(mat){
  mat-rep(1, nrow(mat)) %*% t(colMeans(mat))
})

benchmark( 
  frlp.c=frlp.c(X),
  mat=mat.c(X),
  swp=swp.c(X),
  scl=scl.c(X), 
  matmult=matmult.c(X),
  replications=10,
  order=c('replications', 'elapsed'))

The matmult function seems to be new winner! I really want to try these out on a 5e+08 element matrix, but I keep running out of RAM.

     test replications elapsed relative user.self sys.self user.child sys.child
5 matmult           10   11.98    1.000      7.47     4.47         NA        NA
1  frlp.c           10   35.05    2.926     31.66     3.32         NA        NA
2     mat           10   50.56    4.220     44.52     5.67         NA        NA
4     scl           10   58.86    4.913     50.26     8.42         NA        NA
3     swp           10   61.25    5.113     51.98     8.64         NA        NA
  • 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-11T19:30:24+00:00Added an answer on June 11, 2026 at 7:30 pm

    This seems to be about twice as fast as sweep().

    X - rep(1, nrow(X)) %*% t(colMeans(X))
    
    X <- matrix(sample(1:10, 5e+06, replace=TRUE), ncol=10000)
    system.time(sweep(X, 2, colMeans(X)))
       user  system elapsed 
       0.33    0.00    0.33 
    system.time(X - rep(1, nrow(X)) %*% t(colMeans(X)))
       user  system elapsed 
       0.15    0.03    0.19 
    

    DWin edit: When I did this with a smaller matrix than the OP used (only 5e+07) I get these timings, where Josh’s is mat2 (The larger one overflowed into virtual memory on my Mac w/ 32GB and needed to be terminated) :

      test replications elapsed relative user.self sys.self user.child sys.child
    2 mat2            1   0.546 1.000000     0.287    0.262          0         0
    3  mat            1   2.372 4.344322     1.569    0.812          0         0
    1 frlp            1   2.520 4.615385     1.720    0.809          0         0
    4  swp            1   2.990 5.476190     1.959    1.043          0         0
    5  scl            1   3.019 5.529304     1.984    1.046          0         0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large matrix that I would like to convert to sparse CSR
I have a large matrix from which I would like to gather a collection
I have a large scipy.sparse.csc_matrix and would like to normalize it. That is subtract
I have a large matrix from which I would like to randomly extract a
I have a large csv of similarities between keywords that I would like to
I have a large m *n sparse matrix Y. I would like to normalize
I have a large matrix of 0 s and 1 s, that is mostly
I have large images displayed in a grouped tableview. I would like the images
Suppose I have a large matrix: M <- matrix(rnorm(1e7),nrow=20) Further suppose that each column
i have large numbers of text files and i am in problem that i

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.