I found out that if I have to calculate huge arrays with most of the elements being zero( let's say each row has 10000 elements and only 8 are non zero) and the non zero elements' positions being given by an If or a Which, the most straight forward way to go is:
Table[...,{i,1,nn},{j,list[i]}]
instead of
Table[If[MemberQ[list[i],j]==True,...],{i,1,nn},{j,1,nn}]
But the problem is that, list[i] does not fix the position in the array, but only the value, the positions being [1,Length[list[i]]]. Let us ilustrate this with an easy example:
Slow way:
A = Table[
Which[MemberQ[{1, 3, 5}, j] == True, 2*i + j, True, 0], {i, 1, 2}, {j,
5}] // MatrixForm
(*(3 0 5 0 7
5 0 7 0 9)*)
Fast way:
B=Table[2*i + j, {i, 1, 2}, {j, {1, 3, 5}}] // MatrixForm
(*(3 5 7
5 7 9)*)
So, I would like to get the matrix A but doing it B way somehow.
(The condition for j, is an example, but in the real code is a rather complicated condition that varies with i.)
Does anybody see how this could be done?
Thanks
SparseArray
? $\endgroup$x == True
;) instead you can writex
$\endgroup$