I am trying to find a sparse matrix of the size Col x Rows = 16777216 x 1024
where all the elements are 0
except the ones given by col2
and row2
. This is related to a previous question I asked: Link to the question
The answer by kglr solved my problem for smaller size of the sparse matrix but when I use bigger sizes of Rows, Col, col2, row2
, as in the code given below, the computation goes on forever with no way of knowing when this would complete.
Col = Flatten[Outer[{#4, #3, #2, #1} &, Range[0, 1023, 1], Range[0, 31, 1], {1}, DeleteCases[Range[256, -256, -1], 0, Infinity], 1], 3];
Rows = Flatten[Outer[{#1, #2} &, DeleteCases[Range[-16, 16, 1], 0, Infinity], DeleteCases[Range[-16, 16, 1], 0, Infinity], 1], 1];
col2 = Flatten[Outer[{#4, #3, #2, #1} &, RandomChoice[Range[0, 1023, 1], 64], RandomChoice[Range[0, 31, 1], 16], {1}, RandomChoice[DeleteCases[Range[256, -256, -1], 0, Infinity], 61], 1], 3];
row2 = Rows;
positions = Tuples[{Flatten@Position[Col, Alternatives @@ col2],
Flatten@Position[Rows, Alternatives @@ row2]}];
SparseMat = SparseArray[positions -> (f[Flatten[{Col[[#[[1]]]], Rows[[#[[2]]]]}]] & /@
positions), Length /@ {Col, Rows}];
My question is how can I speed up this computation?. Is there a faster way to do this?.
f
is? $\endgroup$f
is a function but in this code it is just a variable. I thought of computing this matrix first and then define whatf
is, because every single computation off
takes about0.02
seconds. $\endgroup$SparseArray
cannot store the values in a packed array, if the values are not machine numbers. In the end, the values must be calculated anyways. So, please give me a concrete example forf
. $\endgroup$f[{a_, b_, c_, d_, i_, j_}] := a + b + c + d + i + j;
then?. $\endgroup$