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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:18:29+00:00 2026-06-11T13:18:29+00:00

I have a large m *n sparse matrix Y. I would like to normalize

  • 0

I have a large m *n sparse matrix Y. I would like to normalize each row of Y, so that each row has zero mean.

I first tried this. But the mean of each row is also subtracted from the zero entries, which is not what I want.

Ynorm = bsxfun(@minus, Y, Ymean); 

Then I tried this.

[m, n] = size(Y);
nonZeroNum = nnz(Y); 
Ynorm = spalloc(m,n,nonZeroNum); 
for i = 1:m
    Ynorm(i, :) = spfun(@(x)(x - Ymean(i)), Y(i, :));
end

However, this non-vectorized solution is too slow.

I’ve also thought of combining bsxfun and spfun, but didn’t make it.

Does anyone have a vectorized solution?

  • 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-11T13:18:30+00:00Added an answer on June 11, 2026 at 1:18 pm

    Easy, peasy.

    A random sparse matrix.

    A = sprand(100,100,.05);
    

    Get the row means. In case there are no non-zero elements in a row, we will expect 0/0 = NaN, but then that row will never be touched by the next step.

    rowmeans = sum(A,2)./sum(A~=0,2);
    

    Extract the non-zeros.

    [i,j.a] = find(A);
    

    And restore the array, mean subtracted.

    [n,m] = size(A);
    B = sparse(i,j,a - rowmeans(i),n,m);
    

    Now, test it. Don’t forget that floating point arithmetic applies here, so the row means will not be exactly zero, only on the order of eps.

    min(mean(B,2))
    ans =
       (1,1)     -1.5543e-17
    
    max(mean(B,2))
    ans =
       (1,1)      1.1657e-17
    

    Seems about right, and fully vectorized. To convince you that the result truly is sparse and that the zero elements have not been corrupted, here is the result of spy.

    spy(B)
    

    spyplot.jpg

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

Sidebar

Related Questions

I have a large scipy.sparse.csc_matrix and would like to normalize it. That is subtract
I have a large matrix that I would like to convert to sparse CSR
I have a large csv of similarities between keywords that I would like to
I have large images displayed in a grouped tableview. I would like the images
I have a large sparse matrix representing attributes for millions of entities. For example,
I have a very large and very sparse matrix, composed of only 0s and
I have a rather large, dynamic sparse matrix object class to write, and I
I'm new to python, coming from matlab. I have a large sparse matrix saved
I have a large, square sparse matrix in R (about 30M real numbers), and
I'd like to write a function that normalizes the rows of a large sparse

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.