sparse-matrix

How do you edit cells in a sparse matrix using scipy?

谁说胖子不能爱 提交于 2020-01-03 09:58:28
问题 I'm trying to manipulate some data in a sparse matrix. Once I've created one, how do I add / alter / update values in it? This seems very basic, but I can't find it in the documentation for the sparse matrix classes, or on the web. I think I'm missing something crucial. This is my failed attempt to do so the same way I would a normal array. >>> from scipy.sparse import bsr_matrix >>> A = bsr_matrix((10,10)) >>> A[5][7] = 6 Traceback (most recent call last): File "<pyshell#11>", line 1, in

Matlab: how can I perform row operations without brute-force for loop?

不羁的心 提交于 2020-01-03 02:27:15
问题 I need to do function that works like this : N1 = size(X,1); N2 = size(Xtrain,1); Dist = zeros(N1,N2); for i=1:N1 for j=1:N2 Dist(i,j)=D-sum(X(i,:)==Xtrain(j,:)); end end (X and Xtrain are sparse logical matrixes) It works fine and passes the tests, but I believe it's not very optimal and well-written solution. How can I improve that function using some built Matlab functions? I'm absolutely new to Matlab, so I don't know if there really is an opportunity to make it better somehow. 回答1: You

How to wrap Eigen::SparseMatrix over preexistant 3-standard compress row/colum arrays

佐手、 提交于 2020-01-02 16:30:27
问题 NOTE: I allready asked this question, but it was closed because of "too broad" without much explanation. I can't see how this question could be more specific (it deals with a specific class of a specific library for a specific usage...), so I assume that it was something like a "moderator's mistake" and ask it again... I would like to perfom sparse matrix/matrix multiplication using Eigen on sparse matrices. These matrices are already defined in the code I am working on in standard 3-arrays

Extract i and j from a sparse Matrix

一个人想着一个人 提交于 2020-01-02 16:17:53
问题 I can define a sparse Matrix using a vector for i, j, and x: i <- c(1,3:8) j <- c(2,9,6:10) x <- 7 * (1:7) (A <- sparseMatrix(i, j, x = x)) I want to extract the i , j , and x elements from this sparse matrix, so I can re-create the matrix in another package. This is easy with i and x : i <- A@i + 1 x <- A@x (Note that the order of i and x has changed, but their relative association is the same: i=4 is still in the same place as x=21) However, the last element of the sparse matrix is p : "a

how to implement a sparse_vector class

*爱你&永不变心* 提交于 2020-01-02 07:51:12
问题 I am implementing a templated sparse_vector class. It's like a vector, but it only stores elements that are different from their default constructed value. So, sparse_vector would store the lazily-sorted index-value pairs for all indices whose value is not T(). I am basing my implementation on existing sparse vectors in numeric libraries-- though mine will handle non-numeric types T as well. I looked at boost::numeric::ublas::coordinate_vector and eigen::SparseVector . Both store: size_t*

Why are we allowed to create sparse arrays in JavaScript?

◇◆丶佛笑我妖孽 提交于 2020-01-02 07:47:33
问题 I was wondering what the use-cases for code like var foo = new Array(20) , var foo = [1,2,3]; foo.length = 10 or var foo = [,,,] were (also, why would you want to use the delete operator instead of just removing the item from the array). As you may know already, all these will result in sparse arrays. But why are we allowed to do the above thnigs ? Why would anyone want to create an array whose length is 20 by default (as in the first example) ? Why would anyone want to modify and corrupt the

Getting different colors for different numbers using `spy` in Matlab

断了今生、忘了曾经 提交于 2020-01-02 07:29:09
问题 When I use spy to check a sparsity pattern, it doesn't distinguish certain elements from others. Is there any way to do this? Say, for example, elements that are equal to 10 are red and all elements equal to 9 are blue. Can I get this in one spy plot? I've only been able to change the size and style of the plot points. 回答1: Here is how you can do it: spy(a,'k') hold on spy(a==10,'r') spy(a==9,'b') hold off Another way is to use scatter instead of spy : [x,y] = find(a); clr = a(a~=0); scatter

Best way of solving sparse linear systems in C++ - GPU Possible?

谁都会走 提交于 2020-01-02 06:24:06
问题 I am currently working on a project where we need to solve |Ax - b|^2 . In this case, A is a very sparse matrix and A'A has at most 5 nonzero elements in each row. We are working with images and the dimension of A'A is NxN where N is the number of pixels. In this case N = 76800 . We plan to go to RGB and then the dimension will be 3Nx3N . In matlab solving (A'A)\(A'b) takes about 0.15 s, using doubles. I have now done some experimenting with Eigens sparse solvers. I have tried: SimplicialLLT

Adding two `csc` sparse matrices of different shapes in python

僤鯓⒐⒋嵵緔 提交于 2020-01-01 18:55:10
问题 So I have two csc matrices of different shapes that I need to add together. The matrices look like this: current_flows = (7005, 1001) 50.0 (8259, 1001) 65.0 (14007, 1001) 45.0 (9971, 1002) 80.0 : : (69003, 211148) 0.0 result_flows = (7005, 1001) 40 (14007, 1001) 20 (9971, 1002) 35 : : (71136, 71137) 90 final_flows = current_flows + result_flows As illustrated by some of the row and column ID's shown: (7005, 1001), (14007, 1001), (9971, 1002) the matrices do have elements in common. Based on

CSR Matrix - Matrix multiplication

我是研究僧i 提交于 2020-01-01 10:38:50
问题 I have two square matrices A and B I must convert B to CSR Format and determine the product C A * B_csr = C I have found a lot of information online regarding CSR Matrix - Vector multiplication. The algorithm is: for (k = 0; k < N; k = k + 1) result[i] = 0; for (i = 0; i < N; i = i + 1) { for (k = RowPtr[i]; k < RowPtr[i+1]; k = k + 1) { result[i] = result[i] + Val[k]*d[Col[k]]; } } However, I require Matrix - Matrix multiplication. Further, it seems that most algorithms apply A_csr - vector