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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:21:50+00:00 2026-06-12T01:21:50+00:00

A/B in R performs an element-wise division on the matrix. However, if I generate

  • 0

A/B in R performs an element-wise division on the matrix.

However, if I generate a sparse matrix from the Matrix package, and try to divide A/B, I get this error:

> class(N)
[1] "dgCMatrix"
attr(,"package")
[1] "Matrix"
> N/N
Error in asMethod(object) : 
  Cholmod error 'problem too large' at file ../Core/cholmod_dense.c, line 105
> 

Interesting. When the sparse matrix is small in total size, I get this behavior:

> m <- sparseMatrix(i=c(1,2,1,3), j=c(1,1,3,3), x=c(1,2,1,4))
> m/m
3 x 3 Matrix of class "dgeMatrix"
     [,1] [,2] [,3]
[1,]    1  NaN    1
[2,]    1  NaN  NaN
[3,]  NaN  NaN    1
> 

But when it’s moderately sized (~ 20000 elements), I get the Cholmod error.

Is there a workaround or a more proper way to do element-wise division on sparse matrices in R?

  • 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-12T01:21:52+00:00Added an answer on June 12, 2026 at 1:21 am

    The problem with element-wise division is that if your matrices are both sparse, then you’ll have a lot of Inf and NaN in the result, and these make it dense. That’s why you get the out-of-memory errors.

    If you want to replace Inf and NaN with zeros in the result, then the solution is relatively easy, you just get the summary() of both matrices and work with the indices and values directly.

    You’ll need to restrict the A and B index vectors to their intersection and perform the division on that. To get the intersection of index pairs, one can use merge().

    Here is a quick and dirty implementation:

    # Some example data
    A <- sparseMatrix(i=c(1,1,2,3), j=c(1,3,1,3), x=c(1,1,2,3))
    B <- sparseMatrix(i=c(3,2,1), j=c(3,2,1), x=c(3,2,1))
    
    sdiv <- function(X, Y, names=dimnames(X)) {
      sX <- summary(X)
      sY <- summary(Y)
      sRes <- merge(sX, sY, by=c("i", "j"))
      sparseMatrix(i=sRes[,1], j=sRes[,2], x=sRes[,3]/sRes[,4],
                   dimnames=names)
    }
    
    sdiv(A, B)
    # 3 x 3 sparse Matrix of class "dgCMatrix"
    #           
    # [1,] 1 . .
    # [2,] . . .
    # [3,] . . 1
    

    Thanks to flodel for the suggestion about using summary and merge.

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

Sidebar

Related Questions

i'd like to perform element-wise functions on boost matrix and vector types, e.g. take
There's List(T).ForEach() that performs an action on each element of the list. However MSDN
I am looking for a way to vectorize a combined matrix multiplication and element-wise
what does this code performs? if(object.jquery){ .... } I don't know what jQuery attribute
I have a jQuery function that performs slideDown anitmation on a certain element when
The following code performs .css({background:black}); on all elements with class=hole , however I'm trying
I want to turn down the opacity on an element when a user performs
I am performing a stencil computation on a matrix I previously read from a
I want to perform an element wise multiplication, to multiply two lists together by
I have an INPUT element of the TEXT type and I want to perform

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.