sparse-matrix

Sparse Matrix Assignment becomes very slow in Matlab

冷暖自知 提交于 2019-12-20 02:34:58
问题 I am filling a sparse matrix P (230k,290k) with values coming from a text file which I read line by line, here is the (simplified) code while ... C = textscan(text_line,'%d','delimiter',',','EmptyValue', 0); line_number = line_number+1; P(line_number,:)=C{1}; end the problem I have is that while at the beginning the P(line_number,:)=C{1}; statement is fast, after a few thousands lines become exterely slow, I guess because Matlab need to find the memory space to allocate every time. Is there a

Matlab - calculating max eigenvalue of a big sparse (A'*A) matrix

淺唱寂寞╮ 提交于 2019-12-19 07:35:18
问题 I have a big ( 400K*400K ) sparse matrix and I need to calculate the largest eigenvalue of A'*A . The problem is that Matlab can't even calculate A' due to memory problems. I also tried [a,b,c] = find(A) and then transpose by creating a transpose sparse matrix, but although the find() works, the sprase creation doesn't. Is there a nice solution for this? it can be either in a matlab function or in another technique to calculate the largest eigenvalue for this kind of multiplication. Thanks.

Matlab: fast way to sum ones in binary numbers with Sparse structure?

泪湿孤枕 提交于 2019-12-19 05:08:49
问题 Most answers only address the already-answered question about Hamming weights but ignore the point about find and dealing with the sparsity. Apparently the answer by Shai here addresses the point about find -- but I am not yet able to verify it. My answer here does not utilise the ingenuity of other answers such as the bitshifting but good enough example answer. Input >> mlf=sparse([],[],[],2^31+1,1);mlf(1)=10;mlf(10)=111;mlf(77)=1010; >> transpose(dec2bin(find(mlf))) ans = 001 000 000 011

why is row indexing of scipy csr matrices slower compared to numpy arrays

可紊 提交于 2019-12-19 04:42:55
问题 I'm not sure what I am doing wrong but it seems that row indexing a scipy csr_matrix is approximately 2 folds slower compared to numpy arrays (see code below). Shouldn't row indexing of csr matrices be faster than dense matrices because only few non-zero elements are extracted like in the case below ? Are there tricks to make row indexing faster for scipy csr matrices ? import numpy as np import timeit from scipy.sparse import csr_matrix # Generate random matrix A = np.random.rand(5000, 1000)

Creating sparse matrix in MEX

陌路散爱 提交于 2019-12-18 16:52:06
问题 How to create a 2d sparse matrix in a MEX-file written in C. After creating the matrix how to access the elements individually like in C , say mat[i][j] ? I tired using mxCreateNumericArray function but I wasn't able to access the elements and also make it as a sparse matrix. Please help 回答1: See this page on mxCreateSparse. Then you'll want to look at mxSetPr, mxSetIr and mxSetJc and the corresponding "get" versions. Here's an example of how to allocate a sparse matrix. I realize this is an

scipy.sparse : Set row to zeros

假如想象 提交于 2019-12-18 15:58:06
问题 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: 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 ,

Efficient way to set elements to zero where mask is True on scipy sparse matrix

爱⌒轻易说出口 提交于 2019-12-18 15:49:04
问题 I have two scipy_sparse_csr_matrix 'a' and scipy_sparse_csr_matrix(boolean) 'mask', and I want to set elements of 'a' to zero where element of mask is True. for example >>>a <3x3 sparse matrix of type '<type 'numpy.int32'>' with 4 stored elements in Compressed Sparse Row format> >>>a.todense() matrix([[0, 0, 3], [0, 1, 5], [7, 0, 0]]) >>>mask <3x3 sparse matrix of type '<type 'numpy.bool_'>' with 4 stored elements in Compressed Sparse Row format> >>>mask.todense() matrix([[ True, False, True]

Determining the byte size of a scipy.sparse matrix?

雨燕双飞 提交于 2019-12-18 12:47:38
问题 Is it possible to determine the byte size of a scipy.sparse matrix? In NumPy you can determine the size of an array by doing the following: import numpy as np print(np.zeros((100, 100, 100).nbytes) 8000000 回答1: A sparse matrix is constructed from regular numpy arrays, so you can get the byte count for any of these just as you would a regular array. If you just want the number of bytes of the array elements: >>> from scipy.sparse import csr_matrix >>> a = csr_matrix(np.arange(12).reshape((4,3)

Get constraints in matrix format from gurobipy

冷暖自知 提交于 2019-12-18 11:44:34
问题 I coded my model in gurobipy and I want to get the matrix of constraints and vector of cost. Is there any way to access those? 回答1: From the python API, there's no single function to get the matrix coefficients from a Gurobi model, but it's not to hard to write one yourself. It is convenient to have lists of your variables and constraints. If you have a gurobi model in variable m dvars = m.getVars() constrs = m.getConstrs() will give you the list of variables and constraints. You can then use

Improving a badly conditioned matrix

瘦欲@ 提交于 2019-12-18 11:19:48
问题 I have a badly conditioned matrix, whose rcond() is close to zero, and therefore, the inverse of that matrix does not come out to be correct. I have tried using pinv() but that does not solve the problem. This is how I am taking the inverse: X = (A)\(b); I looked up for a solution to this problem and found this link (last solution) for improving the matrix. The solution there suggests to use this: A_new = A_old + c*eye(size(A_old)); Where c > 0 . So far employing this technique works in