I have this sparse matrix of the following form

Lets take an example of 5x10 matrix

```
1 2 3 4 5 6 7 8 9 10
1 1 1 0 0 0 0 0 0 0 0
2 0 1 0 0 0 0 0 0 0 0
3 .............................
4 .............................
5 .............................
```

From this sparse matrix, I want to create a cell array C of form

```
C{1} 1
C{2} = [1,2]
...........
...........
...........
```

My sparse matrix is high dimensional like 40000 by 790000. How can I do it efficiently in matlab. I can definitely use a loop and do it inefficiently. But I want the most efficient. Suggestions?

Let me get the party started... let's start with the basics:

```
tic;
sz = [ 400 7900]; % hehe...
aMat = sparse(randi(sz(1),[1000 1]),randi(sz(2),[1000 1]),1,sz(1),sz(2));
aCell = mat2cell(aMat,ones([sz(1) 1]));
preC = cellfun(@(x) x(x~=0), aCell,'UniformOutput',false);
C = cellfun(@(x) find(x), preC,'UniformOutput',false);
toc
```

Use `find`

to get the indices and `accumarray`

to group them by columns:

```
[ii, jj] = find(A);
C = accumarray(jj, ii, [], @(v) {v.'});
```

**Benchmarking**

```
%// Random sparse matrix. Code adapted from @teng's answer
sz = [4e4 79e4];
nz = 1e5; %// number of nonzeros
A = sparse(randi(sz(1),[nz 1]),randi(sz(2),[nz 1]),1,sz(1),sz(2));
tic;
[ii, jj] = find(A);
C = accumarray(jj, ii, [], @(v) {v.'});
toc
```

Results:

For

`nz = 1e4`

:`Elapsed time is 0.099657 seconds.`

For

`nz = 1e5`

:`Elapsed time is 0.756234 seconds.`

For

`nz = 1e6`

:`Elapsed time is 5.431427 seconds.`

Similar Questions

I'm looking for a Sparse Matrix library I can use from Ruby. I'm currently using the GNU Scientific Library bindings provided by the gsl gem, but my application would be better optimized if I used a

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'm trying to solve a rather large linear programming problem in Mathematica, but for some reason the bottleneck is setting up the array of linear constraints. My code for initializing the matrix look

I have a 3007 x 1644 dimensional matrix of terms and documents. I am trying to assign weights to frequency of terms in each document so I'm using this log entropy formula http://en.wikipedia.org/wiki/

I have a 5000 *5000 sparse matrix with 4 different values. I want to visualise the nonzero elements with 4 different colors such that I can recognise the ratio of this values and the relationships bet

This question already has an answer here: Sparse matrices / arrays in Java 11 answers I need to implement a sparse matrix as efficiently memory-wise as I can in Java.I receive a matrix with mor

I have a very large and sparse matrix of size 180GB(text , 30k * 3M) containing only the entries and no additional data. I have to do matrix multiplication , inversion and some similar linear algebra

I ran into the following issue trying to vstack two large CSR matrices: /usr/lib/python2.7/dist-packages/scipy/sparse/coo.pyc in _check(self) 229 raise ValueError('negative row index found') 230 if s

I need to perform a set of operations on a scipy sparse matrix in a Cython method. To efficiently apply these I need access to lil_matrix representation. The lil (linked-list sparse matrix) data repre

I have a sparse matrix (dgCMatrix) as the result of fitting a glmnet. I want to write this result to a CSV but can't use write.table() the matrix because it can't coerced into a data.frame. Is there a

When dealing with sparse matrices, how do I convert Matrix Market format into CRS (Compressed Row Storage)?

I have a little problem, I would like to convert a matrix 10*10 in a CSR or COO sparse matrix/format. The matrix is: 1.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 -0.45 0.10 -0.45 0.00 0.00 0.00

My system is best described by a diagonal sparse matrix (Poisson). I have my diagonal sparse matrix, however, I want to change the boundary conditions (ie the edges of my matrix) to zero. It must be

I am trying to do some (k-means) clustering on a very large matrix. The matrix is approximately 500000 rows x 4000 cols yet very sparse (only a couple of 1 values per row). I want to get around 2000

I researched a lot on this but couldn't find a practical solution to this problem. I am using scipy to create csr sparse matrix and want to substract this matrix from an equivalent matrix of all ones.

I have a cell A composed of: 'hello' 'world' 'test' I would like to convert this to a matrix. I tried cell2mat(A) Which returns Error using cat Dimensions of matrices being concatenated are not cons

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

When trying to run the cor() function on sparse matrices (of either type dgCMatrix or dgTMatrix) I get the following error: Error in cor(x) : supply both 'x' and 'y' or a matrix-like 'x' Converting m

I have a huge sparse matrix in Scipy and I would like to replace numerous elements inside by a given value (let's say -1). Is there a more efficient way to do it than using: SM[[rows],[columns]]=-1 H

I have a pretty large matrix M and I am only interested in a few of the columns. I have a boolean vector V where a value of 1 represents a column that is of interest. Example: -1 -1 -1 7 7 -1 -1 -1 7

I am working on an algorithm that uses diagonal and first off-diagonal blocks of a large (will be e06 x e06) block diagonal sparse matrix. Right now I create a dict that stores the blocks in such a wa

I have two sparse matrices, m1 and m2: > m1 <- Matrix(data=0,nrow=2, ncol=1, sparse=TRUE, dimnames=list(c(b,d),NULL)) > m2 <- Matrix(data=0,nrow=2, ncol=1, sparse=TRUE, dimnames=list(c

I am working with Matlab, and I have a cell array with 5 columns and half million rows and a double matrix with columns and about a quarter million rows. Let’s call them A and B. A has both string and

I have a large sparse matrix, and I want to permute its rows or columns to turn the original matrix into a block diagonal matrix. Anyone knows which functions in R or MATLAB can do this? Thanks a lot.

I need to create a matrix with values from a numpy array. The values should be distributed over the matrix lines according to an array of indices. Like this: >>> values array([ 0.73620381, 0.

I need to store word co-occurrence counts in several 14000x10000 matrices. Since I know the matrices will be sparse and I do not have enough RAM to store all of them as dense matrices, I am storing th

I have a very large Scipy sparse (csr) matrix. I can't use M.toarray() since it triggers ValueError: array is too big. Is there a way of saving a Scipy sparse matrix in Python to be read in Matlab? I

I'm wondering whether there is a way to simply add a dense vector to all the rows of a sparse matrix represented as a csr_matrixin scipy.sparse and returning a sparse matrix, ie trying to sum only the

In Matlab, at what point is having a sparse array better than a normal array if I still have a lot of calculations to do on it, and about 25% of the array are non-zeros?

I have this huge sparse matrix A of size 2 million by 10 thousand. I want to index particular 1000 rows (index) from this matrix. If I do B = A(index,:); it takes some time. Is there a better quick

I am looking for sparse matrix representation that allow for efficient row and column swaping. The classic representation (by compressed row,compressed column or triplets) seems to only allow to perfo

I'm writing a program that will convert a sparse matrix to Blocked Compressed Row Storage BCRS. I know how to acquire Rowptr, Colind(although not in the code) and A_f. Code: p = 0; for (i = 0; i <

I want to do SVD on a sparse matrix by using scipy: from svd import compute_svd print(The size of raw matrix: +str(len(raw_matrix))+ * +str(len(raw_matrix[0]))) from scipy.sparse import dok_matrix

How can a sparse matrix - matrix product be calculated? I know the 'classic' / mathematical way of doing it, but it seems pretty inefficient. Can it be improved? I thought about storing the first matr

I need to do matrix operations (mainly multiply and inverse) of a sparse matrix SparseMat in OpenCV. I noticed that you can only iterate and insert values to SparseMat. Is there an external code I can

I have a 1xm cell array A{}, with each element of the array being NxN matrix and a matrix W(N1,m). I need to calculate Sum(j) = W(j,1)*A{1,1} + W(j,2)*A{1,2} and I am doing the following: for j=1:N1

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'm involved in the resolution of a system of the type Ax = b, where A is a square sparse matrix, x is the vector of the unknows (I have to compute it) and b is a vector of all zeros excpet for the la

How to efficiently combined cell array vectors with different length into a matrix, filling the vectors to max length with 0s or NaNs? It would be a nice option for cell2mat(). For example, if I have

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

I want to create a custom optimized matrix operation (a smart kronecker product based on what I know about the sparse matrices i'm using) using MathNet.numerics for csharp. Is there an accessor to get

I have this sparse matrix of size 20 millionx20million in matlab. I want to get around 40000 specific rows from this matrix. If I do new_data = data_original(index,:) where index consists of the rows

Imagine I have a series of different sized column vectors inside an array and want to group them into a matrix by padding the empty spaces with NaN. How can I do this? There is already an answer to a

I'm trying to transpose a sparse matrix in c++. I'm struggling with the traversal of the new transposed matrix. I want to enter everything from the first row of the matrix to the first column of the n

I'm working in a program for Power System analysis and I need to work with sparse matrices. There is a routine where I fill a sparse matrix just with the following call: self.A = bsr_matrix((val, (row

I see with new Eigen 3.2, you can get row, column or even block from a sparse matrix, is there a way to set any of these to 0? Eigen::SparseMatrix<float, Eigen::RowMajor> A( 5, 5 ); A.block(1, 1

I am doing a text classification task with R, and I obtain a document-term matrix with size 22490 by 120,000 (only 4 million non-zero entries, less than 1% entries). Now I want to reduce the dimension

I want to augment the scipy.sparse.csr_matrix class with a few methods and replace a few others for personal use. I am making a child class which inherits from csr_matrix, as such: class SparseMatrix(

I am using CHOLMOD in SuiteSparse to factor an N by N large band-diagonal matrix that is relatively sparse, ie, it contains only a few diagonals that are non-zero. The sparsity of the matrix is set by

I am trying to subsample a scipy sparse matrix as a numpy matrix like this to get every 10th row and every 10th column: connections = sparse.csr_matrix((data,(node1_index,node2_index)), shape=(dimensi