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?

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)
```

Similar Questions

I'm looking for any standard C program that uses OpenMP APIs for a sparse matrix-vector or matrix-matrix multiplications. Can anyone let me know if there are any such programs.

I did a sparse qr decomposition with Matrix package in R like a <- Matrix(runif(20), nrow = 5, sparse = T) a[3:5,] <- 0 #now **a** is a 5X4 matrix b <- qr.R(qr(a), complete = T) #but now **

I'm looking for an a command or trick to convert two arrays to a sparse matrix. The two arrays contain x-values and y-values, which gives a coordinate in the cartesian coordinate system. I want to gro

How can I represent an image as a matrix in Matlab?

Say I would like to remove the diagonal from a scipy.sparse.csr_matrix. Is there an efficient way of doing so? I saw that in the sparsetools module there are C functions to return the diagonal. Based

Is there a way to compute the power of a sparse matrix in matlab without converting it to a full matrix. If I try b = a^0.5 where a is a sparse matrix, I get the error Use full(x)^full(y).. However

I want to initial a sparse matrix with numpy array. The numpy array contains NaN as zero for my program, the code to initial a sparse matrix as following: a= np.array([[np.NaN,np.NaN,10]]) zero_a= np.

If I have a matrix like this A = [1 2; 3 4]; I can use interp2 to interpolate it like this newA = interp2(A,2); and I get a 5x5 interpolated matrix. But what if I have a matrix like this: B = zeros(

I created a compressed sparse matrix, but while accessing to a positive index it complains that the index is negative: import scipy.sparse as sparse B= sparse.csc_matrix((110111213141516, 25)) B[11011

I have time data with irregular intervals and I need to convert it to a sparse matrix for use with a graphing library. The data is currently in the following format: { :series1 => [entry, entry, en

I'm looking for a library to do huge Sparse Matrix x Vector multiplication. The matrix itself will almost fill the RAM. I've found Eigen3, OSKI and some basic Sparse BLAS implementations. Are there ot

Given the matrix: A=[1 2 3; 4 5 6; 7 8 9] how would you use the for loop to compute the sum of the elements in matrix? write a one line MATLAB command using the function sum() to sum the matrix elem

I have a matrix like A = [ 0 1 1;0 0 0;1 1 0] For this, I wish to know indices of 1's in such a manner that: For row 1, I need values of indices of column where its coming equal 1. Similarly for row

There are at least two sparse matrix packages for R. I'm looking into these because I'm working with datasets that are too big and sparse to fit in memory with a dense representation. I want basic lin

I've read several topics, but I'm lost. I'm quite new to this. I want to store huge sparse matrix and have several idea's but can choose between them. Here's my needs: Adjacency matrix of approx. 50

I am using the C++ Eigen 3 library in my program. In particular, I need to multiply two Eigen sparse matrix and store the result into another Eigen sparse matrix. However, I noticed that if the some e

I have a very large (about 91 million non-zero entries) sparseMatrix() in R that looks like: > myMatrix a b c a . 1 2 b 1 . . c 2 . . I would like to convert it to a triangular matrix (upper or lo

Let's say I have a 10 x 10 matrix. What I do then is iterate through the whole matrix with an 3 x 3 matrix (except from the edges to make it easier), and from this 3 x 3 matrix I get the mean/average

I have to calculate Covaiance Matrix to do PCA, but whenever I compae my Covariance Matrix result with the covariance matrix resul from matlab (using Cov function), it gives different result. So it me

I've created a TermDocumentMatrix from the tm library in R. It looks something like this: > inspect(freq.terms) A document-term matrix (19 documents, 214 terms) Non-/sparse entries: 256/3810 Sparsi

I have a sparse matrix Formal class 'dgCMatrix' [package Matrix] with 6 slots ..@ i : int [1:37674] 1836 2297 108 472 1735 1899 2129 2131 5 67 ... ..@ p : int [1:3417] 0 2 8 22 25 35 44 45 45 47 ..

I am trying to create a large sparse matrix, 10^5 by 10^5 in R, but am running into memory issues. > Matrix(nrow=1e5,ncol=1e5,sparse=TRUE) Error in Matrix(nrow = 1e+05, ncol = 1e+05, sparse = TRUE)

I have got an output using sparse matrix in python, i need to store this sparse matrix in my hard disk, how can i do it? if i should create a database then how should i do?? this is my code: import nl

If I am using the sparse.lil_matrix format, how can I remove a column from the matrix easily and efficiently?

I need to insert the zero elements in any sparse matrix in the Matrix Market format (but already without the headers). The first column is the number of the ROW, the second columns is the number of th

I have a sparse logical matrix, which is quite large. I would like to draw random non-zero elements from it without storing all of its non-zero elements in a separate vector (eg. by using find command

In a scipy program I'm creating a dia_matrix (sparse matrix type) with 5 diagonals. The centre diagonal the +1 & -1 diagonals and the +4 & -4 diagonals (usually >> 4, but the principle i

I have a cell array myBasis of sparse matricies B_1,...,B_n. I want to evaluate with Matlab the matrix Q(i,j) = trace (B^T_i * B_j). Therefore, I wrote the following code: for i=1:n for j=1:n B=myBasi

This Sparse Matrix and its 3-Tuple representation is not getting into my head... Either its bit tricky or my resources from where I am studying are really not that good... here is the URI Sparse Matri

I have an N x N sparse matrix in Matlab, that has cell values indexed by (r,c) pairs such that r and c are unique id's. The problem is, that after converting this matrix into Python, all of the indic

I'm trying to write a matrix to sparse matrix conversion program. But, there is a problem in my scan_matrix function.I couldn't figure out where is the problem.According to Nemiver(debugging program)

This question already has an answer here: Matlab: add vector to matrix 2 answers I am trying to add a matrix with another matrix in MATLAB. The first matrix looks like this: 0.0963928845397177

We have an application that stores a sparse matrix. This matrix has entries that mostly exist around the main diagonal of the matrix. I was wondering if there were any efficient algorithms (or existin

How in Matlab we can form a matrix X, 1000 by 1000, which is sparse with, say, 5% of independent Bernoulli +-1 nonzero entries? I.e. such a matrix would have rho = ||X||_0/10^6 = 0.05.

In another post regarding resizing of a sparse matrix in SciPy the accepted answer works when more rows or columns are to be added, using scipy.sparse.vstack or hstack, respectively. In SciPy 0.12 the

I have a large sparse graph that I am representing as an adjacency matrix (100k by 100k or bigger), stored as an array of edges. An example with a (non-sparse) 4 by 4 matrix: 0 7 4 0 example_array = [

I need to create spare matrices with variable elements. Unfortunately, sparse matrix index operations are very slow. Is there any way to speed up the process? Maybe there are some tricks that I don't

I'm using Python, Numpy and Scipy packages to do matrix computations. I am attempting to perform the calculation X.transpose() * W * X where X is a 2x3 dense matrix and W is a sparse diagonal matrix.

I have a 30000x14000 sparse matrix in MATLAB, which I need to use in another program. Calling save won't write this as ASCII (not supported). Calling full() on this monster results in an Out of Memory

I need a 3D matrix in matlab, I have another 2D matrix (7570x3) too, The 3D matrix should have zero number except of all dimensions in 2D matrix that should have 1 value. How can I do that. i.e. 2D m

I would like to set n entries of matrix to zero, below is my attempt to solve this problem, by generation uique pairs of integers. How to generate unique pairs of integers from uniform distribution (x

I've got a sparse Matrix in R that's apparently too big for me to run as.matrix() on (though it's not super-huge either). The as.matrix() call in question is inside the svd() function, so I'm wonderin

I've got an n-by-k sized matrix, containing k numbers per row. I want to use these k numbers as indexes into a k-dimensional matrix. Is there any compact way of doing so in MATLAB or must I use a for

I have a 3D matrix (tensor) which can be accessed as m[x][y][z] I want to find and save only the non zero elements.. with their index.. (as this matrix is sparse).. So an equivalent code will be: fo

This is my current matlab code: a = load('m1.txt'); b = load('m2.txt'); c = a*b; fid = fopen('Matrix.txt','wt'); for ii = 1:size(c,1) fprintf(fid,'%g\t',c(ii,:)); fprintf(fid,'\n'); end fclose(fid) B

I have a matrix in MATLAB with zeroes and I would like to get another matrix with the first N non-zero elements in each row. Let's say for example N = 3, and the matrix is A = [ 0 0 2 0 6 7 9; 3 2 4 7

I have a 1028 by 18 matrix in matlab.I want to calculate the mean of 1st and 2nd row by column values,3rd and 4th and so on in Matlab and get a new matrix with the mean values.

type(A) <class 'scipy.sparse.csc.csc_matrix'> A.shape (8529, 60877) print A[0,:] (0, 25) 1.0 (0, 7422) 1.0 (0, 26062) 1.0 (0, 31804) 1.0 (0, 41602) 1.0 (0, 43791) 1.0 print A[1,:] (0, 7044) 1.0

This question has two parts (maybe one solution?): Sample vectors from a sparse matrix: Is there an easy way to sample vectors from a sparse matrix? When I'm trying to sample lines using random.sample

I am converting from a scipy sparse matrix to a dense matrix and adding that to an ndarray using a += operator and I am getting a broadcast error. The ndarray has a shape (M,) while the dense matrix h