sparse-array

extract sparse rows from sparse matrix in r

不羁岁月 提交于 2019-12-11 11:43:55
问题 I have a large sparse matrix to analyze in R. For instance: i <- c(1,3:8); j <- c(2,9,6:10); x <- 7 * (1:7) (A <- sparseMatrix(i, j, x = x)) [1,] . 7 . . . . . . . . [2,] . . . . . . . . . . [3,] . . . . . . . . 14 . [4,] . . . . . 21 . . . . [5,] . . . . . . 28 . . . [6,] . . . . . . . 35 . . [7,] . . . . . . . . 42 . [8,] . . . . . . . . . 49 I want to extract the i -th row from this matrix, as a sparse vector. If I write (x=A[1,]) I obtain [1] 0 7 0 0 0 0 0 0 0 0 but what I would like is

Matlab: First Non-zero element of each row or column

情到浓时终转凉″ 提交于 2019-12-09 16:26:38
问题 For example, A = [ -1 0 -2 0 0 2 8 0 1 0 0 0 3 0 -2 0 -3 2 0 0 1 2 0 0 -4]; how can I get a vector of the first nonzero elements of each row? 回答1: You can use max : >> [sel, c] = max( A ~=0, [], 2 ); Rows for which sel equalse zero - are all zeros and the corresponding column in c should be ignored. Result: >> [sel c]= max( A~=0, [], 2 ) sel = 1 1 1 1 1 c = 1 1 3 2 1 In order to find the first non-zero row index (for each column) you just need to apply max on the first dimension: >> [sel r] =

Sparse array support in HDF5

一笑奈何 提交于 2019-12-09 09:07:07
问题 I need to store a 512^3 array on disk in some way and I'm currently using HDF5. Since the array is sparse a lot of disk space gets wasted. Does HDF5 provide any support for sparse array ? 回答1: Chunked datasets (H5D_CHUNKED) allow sparse storage but depending on your data, the overhead may be important. Take a typical array and try both sparse and non-sparse and then compare the file sizes, then you will see if it is really worth. 回答2: One workaround is to create the dataset with a compression

Which is the best way to implement a sparse vector in Java?

雨燕双飞 提交于 2019-12-08 06:57:03
问题 Which is the best way to implement a sparse vector in Java? Of course the good thing would be to have something that can be manipulated quite easily (normalization, scalar product and so on) Thanks in advance 回答1: MTJ has a Sparse Vector class. It has norm functions (1-norm 2-norm and ∞-norm) and dot product functions. 回答2: JScience has a SparseVector implementation that is part of its linear algebra package. 回答3: You can also try to look at la4j's CompressedVector implementation. It uses

Is Cocoa's NSMutableArray sparse?

一世执手 提交于 2019-12-08 06:28:19
问题 If I create an NSMutableArray that might have up to 2^16 elements, but will mostly be empty, will I be wasting space or is NSMutableArray implemented as a sparse array? 回答1: Elements in an NSArray can't be empty and there's no "default" value. To represent nil , you'd usually use the singleton [NSNull null] , which is still a reference to an object so it consumes memory (the pointer). I'd consider using NSDictionary (or NSMutableDictionary ) with numeric ( NSNumber ) keys instead. 回答2: No

How to store sparsearray in bundle

被刻印的时光 ゝ 提交于 2019-12-07 05:06:12
问题 I have a SparseArray<myObject> and want to store it in bundle in onSaveInstanceState method in my activity and restore it in oncreate . I found putSparseParcelableArray method for put SparseArray in bundle and did this in onSaveInstanceState method: bundle.putSparseParcelableArray("mySparseArray", mySparseArray); But eclips shows this error: The method putSparseParcelableArray(String, SparseArray<? extends Parcelable>) in the type Bundle is not applicable for the arguments (String,

Sparse O(1) array with indices being consecutive products

孤街醉人 提交于 2019-12-06 10:09:46
I'd like to pre-calculate an array of values of some unary function f . I know that I'll only need the values for f(x) where x is of the form of a*b , where both a and b are integers in range 0..N . The obvious time-optimized choice is just to make an array of size N*N and just pre-calculate just the elements which I'm going to read later. For f(a*b) , I'd just check and set tab[a*b] . This is the fastest method possible - however, this is going to take a lot of space as there are lots of indices in this array (starting with N+1 ) which will never by touched. Another solution is to make a

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

一世执手 提交于 2019-12-06 05:06:17
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 compressed row/column strorage. Then I would like to use the Eigen::SparseMatrix class as a wrapper on

How to quickly compact a sparse array with CUDA C?

早过忘川 提交于 2019-12-06 04:31:10
问题 Summary Array [A - B - - - C] in device memory but want [A B C] - what's the quickest way with CUDA C? Context I have an array A of integers on device (GPU) memory. At each iteration, I randomly choose a few elements that are larger than 0 and subtract 1 from them. I maintain a sorted lookup array L of those elements that are equal to 0: Array A: @ iteration i: [0 1 0 3 3 2 0 1 2 3] @ iteration i + 1: [0 0 0 3 2 2 0 1 2 3] Lookup for 0-elements L: @ iteration i: [0 - 2 - - - 6 - - -] -> want

how to implement a sparse_vector class

╄→尐↘猪︶ㄣ 提交于 2019-12-05 22:18:08
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* indices_; // a dynamic array T* values_; // a dynamic array int size_; int capacity_; Why don't they