I am working with a very large sparse matrix (for example) given in what follows:
m = 50; n = 40; o = 30; size = m*n*o;
B = SparseArray[{
{i_, i_} -> RandomReal[], {size, size - 1} ->
2., {i_, j_} /; Abs[i - j] == 5 ->
1., {i_, j_} /; Abs[3 i - j] == 2 -> 2.
}, {size, size}, 0.]
This is a matrix of the size $60000\times 60000$ as a simple instance (in practice I have several of such matrices of higher sizes).
I wish to replace many rows of the matrix $B$ with special rows (here with zero rows) coming from boundary conditions. My list of rows' numbers are given by
index1 = Flatten[Table[{i, j, k}, {i, 1, m}, {j, 1, n}, {k, 1, o}], 2];
Table[ind = index1[[l]];
If[ind[[1]] == 1 || ind[[2]] == n, var[l] = 0, var[l] = l], {l,
Length@index1}];
bounindex = Table[var[l], {l, Length@index1}];
And my zero row is produced by
vector1 = SparseArray@ConstantArray[0, size];
Now for doing the replacement only for the 0 numbers obtained in bounindex
, I write the following replacing rule in a loop as simply as possible:
Table[Which[bounindex[[i]] == 0, B[[i]] = vector1], {i,
size}]; // AbsoluteTiming
This works OK but it takes a tremendous time (in my real problem I have larger sizes)! So I am wondering if there is a speed-up technique to replace the special rows of the matrix $B$ with appropriate rows (that I want).
I would be thankful if some hints for accelerating such a replacement be given.