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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:24:03+00:00 2026-06-10T06:24:03+00:00

Suppose I have a matrix in the CSR format, what is the most efficient

  • 0

Suppose I have a matrix in the CSR format, what is the most efficient way to set a row (or rows) to zeros?

The following code runs quite slowly:

A = A.tolil()
A[indices, :] = 0
A = A.tocsr()

I had to convert to scipy.sparse.lil_matrix because the CSR format seems to support neither fancy indexing nor setting values to slices.

  • 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-10T06:24:05+00:00Added an answer on June 10, 2026 at 6:24 am

    I guess scipy just does not implement it, but the CSR format would support this quite well, please read the wikipedia article on “Sparse matrix” about what indptr, etc. are:

    # A.indptr is an array, one for each row (+1 for the nnz):
    
    def csr_row_set_nz_to_val(csr, row, value=0):
        """Set all nonzero elements (elements currently in the sparsity pattern)
        to the given value. Useful to set to 0 mostly.
        """
        if not isinstance(csr, scipy.sparse.csr_matrix):
            raise ValueError('Matrix given must be of CSR format.')
        csr.data[csr.indptr[row]:csr.indptr[row+1]] = value
    
    # Now you can just do:
    for row in indices:
        csr_row_set_nz_to_val(A, row, 0)
    
    # And to remove zeros from the sparsity pattern:
    A.eliminate_zeros()
    

    Of course this removes 0s that were set from another place with eliminate_zeros from the sparsity pattern. If you want to do that (at this point) depends on what you are doing really, ie. elimination might make sense to delay until all other calculations that might add new zero’s are done as well, or in some cases you may have 0 values, that you want to change again later, so it would be very bad to eliminate them!

    You could in principle of course short-circuit the eliminate_zeros and prune, but that should be a lot of hassle, and might be even slower (because you won’t do it in C).


    Details about eliminiate_zeros (and prune)

    The sparse matrix, does generally not save zero elements, but just stores where the nonzero elements are (roughly and with various methods). eliminate_zeros removes all zeros in your matrix from the sparsity pattern (ie. there is no value stored for that position, when before there was a vlaue stored, but it was 0). Eliminate is bad if you want to change a 0 to a different value lateron, otherwise, it saves space.

    Prune would just shrink the data arrays stored when they are longer then necessary. Note that while I first had A.prune() in there, A.eliminiate_zeros() already includes prune.

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

Sidebar

Related Questions

suppose I have the following matrix a = 2 NaN NaN 4 NaN 3
Suppose I have the following matrix: 01 02 03 06 03 05 07 02
Suppose I have an (m x n) matrix Q, and a row vector r,
Suppose I have a matrix of the following form: Residue Can.Count SideChain XCoord YCoord
Suppose I have the following code: glRotatef(angle, 1.0f, 1.0f, 0.0f); glRotatef(angle, 0.0f, 1.0f, 0.0f);
Suppose I have a matrix such that each row is a standard basis vector,
suppose I have the following matrix a = 76 NaN 122 NaN 78 NaN
suppose I have the following matrix: a = 2 NaN NaN 4 NaN 3
Suppose I have a matrix of weights, and another matrix of data values. Can
Suppose I have a matrix foo as follows: foo <- cbind(c(1,2,3), c(15,16,17)) > foo

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.