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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:02:37+00:00 2026-05-21T09:02:37+00:00

I have two matrices that I want to apply a function to, by rows:

  • 0

I have two matrices that I want to apply a function to, by rows:

matrixA
           GSM83009  GSM83037  GSM83002  GSM83029  GSM83041
100001_at  5.873321  5.416164  3.512227  6.064150  3.713696
100005_at  5.807870  6.810829  6.105804  6.644000  6.142413
100006_at  2.757023  4.144046  1.622930  1.831877  3.694880

matrixB
          GSM82939 GSM82940 GSM82974 GSM82975
100001_at 3.673556 2.372952 3.228049 3.555816
100005_at 6.916954 6.909533 6.928252 7.003377
100006_at 4.277985 4.856986 3.670161 4.075533

I’ve found several similar questions, but not a whole lot of answers: mapply for matrices, Multi matrix row-wise mapply?. The code I have now splits the matrices by row into lists, but having to split it makes it rather slow and not much faster than a for loop, considering I have almost 9000 rows in each matrix:

scores <- mapply(t.test.stat, split(matrixA, row(matrixA)), split(matrixB, row(matrixB)))

The function itself is very simple, just finding the t-value:

t.test.stat <- function(x, y)
{
    return( (mean(x) - mean(y)) / sqrt(var(x)/length(x) + var(y)/length(y)) )
}
  • 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-21T09:02:38+00:00Added an answer on May 21, 2026 at 9:02 am

    Splitting the matrices isn’t the biggest contributor to evaluation time.

    set.seed(21)
    matrixA <- matrix(rnorm(5 * 9000), nrow = 9000)
    matrixB <- matrix(rnorm(4 * 9000), nrow = 9000)
    
    system.time( scores <- mapply(t.test.stat,
        split(matrixA, row(matrixA)), split(matrixB, row(matrixB))) )
    #    user  system elapsed 
    #    1.57    0.00    1.58 
    smA <- split(matrixA, row(matrixA))
    smB <- split(matrixB, row(matrixB))
    system.time( scores <- mapply(t.test.stat, smA, smB) )
    #    user  system elapsed 
    #    1.14    0.00    1.14 
    

    Look at the output from Rprof to see that most of the time is–not surprisingly–spent evaluating t.test.stat (mean, var, etc.). Basically, there’s quite a bit of overhead from function calls.

    Rprof()
    scores <- mapply(t.test.stat, smA, smB)
    Rprof(NULL)
    summaryRprof()
    

    You may be able to find faster generalized solutions, but none will approach the speed of the vectorized solution below.

    Since your function is simple, you can take advantage of the vectorized rowMeans function to do this almost instantaneously (though it’s a bit messy):

    system.time({
    ncA <- NCOL(matrixA)
    ncB <- NCOL(matrixB)
    ans <- (rowMeans(matrixA)-rowMeans(matrixB)) /
      sqrt( rowMeans((matrixA-rowMeans(matrixA))^2)*(ncA/(ncA-1))/ncA +
            rowMeans((matrixB-rowMeans(matrixB))^2)*(ncB/(ncB-1))/ncB )
    })
    #    user  system elapsed 
    #      0       0       0 
    head(ans)
    # [1]  0.8272511 -1.0965269  0.9862844 -0.6026452 -0.2477661  1.1896181
    

    UPDATE
    Here’s a “cleaner” version using a rowVars function:

    rowVars <- function(x, na.rm=FALSE, dims=1L) {
      rowMeans((x-rowMeans(x, na.rm, dims))^2, na.rm, dims)*(NCOL(x)/(NCOL(x)-1))
    }
    ans <- (rowMeans(matrixA)-rowMeans(matrixB)) /
      sqrt( rowVars(matrixA)/NCOL(matrixA) + rowVars(matrixB)/NCOL(matrixB) )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays of System.Data.DataRow objects which I want to compare. The rows
Here's an interesting question :) I have two vectors of matrices which I want
say I want to multiply two matrices together, 50 by 50. I have 2
I have to generate two random sets of matrices Each containing 3 digit numbers
I have two applications written in Java that communicate with each other using XML
I have two identical tables and need to copy rows from table to another.
I have two classes, and want to include a static instance of one class
I have two spreadsheets... when one gets modified in a certain way I want
I have two matrices in MATLAB lets say arr1 and arr2 of size 1000*1000
A have a struct with four very large matrices that correspond to grayscale images.

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.