for example I have a data
Clear[data];
data[n_] :=
Join[RandomInteger[{1, 10}, {n, 2}], RandomReal[1., {n, 1}], 2];
then data[3]
gives
{{4, 8, 0.264842}, {9, 5, 0.539251}, {3, 1, 0.884612}}
in each sublist, first two value is matrix index, the last is matrix element which have to be added together for same matrix index.
I want to transform the data into matrix. Usually I do it like
Clear[toSparse]
toSparse[data_] :=
SparseArray@
Normal@Merge[Thread[data[[;; , 1 ;; 2]] -> data[[;; , -1]]], Total]
I cared about the performance
In[171]:= toSparse[data[1000]]; // AbsoluteTiming
Out[171]= {0.00836793, Null}
In[172]:= toSparse[data[10000]]; // AbsoluteTiming
Out[172]= {0.0644464, Null}
In[173]:= toSparse[data[100000]]; // AbsoluteTiming
Out[173]= {1.35507, Null}
In[174]:= toSparse[data[1000000]]; // AbsoluteTiming
Out[174]= {200.862, Null}
Any faster way to do this?
SparseArray
s, right? So the duplicate only explains the answers given so far, that useSparseArray
s. The reason I point out this is that there are even faster ways than the ones presented here. $\endgroup$toSparse[data_] := . . .
. I'll reopen. I look forward to your answer with a faster method than"TreatRepeatedEntries"
! $\endgroup$