0
votes

I have a sparse matrix in MATLAB which dimensions are: 8970240 x 8970240 = L x L. Let's call it M.

I have to assign to a lot of elements in the matrix the value of 1, for example, given the pair of index i and j: M(i, j) = 1.

I have indices where I want to perform the assignment stored in vectors, this is:

  • One dimension vector V1 stores the row indices (i's).
  • One dimension vector V2stores the column indices (j's).

Now, the issue is that the length of V1 (7004160) is different to the length of V2 (6389760). Which also returns a lot of nonzero elements in my sparse matrix, a total of 7004160 x 6389760 = 44754901401600 = A nonzero elements.

I have tried to construct M this way:

M = sparse(V1, V2, ones(A), L, L)

But it does not work...

Does anybody know how to get around it?

1
v1 and v2 cannot be different sizes! if they are i and j, what happens when you run out of v2? M(v1(7004160) , ?????? ) =1 - Ander Biguri
I know, that is why I am asking for help. Here is the thing is in V1 I have stored the row indices, let's say, I have 1, 5 and 12, for example. Then in V2 I have stored the column indices, let's say, I have 3 and 65. I need to assign one to the following pairs: M(1, 3) , M(1, 65) , M(5, 3), M(5, 65) , M(12, 3) and M(12, 65). I honestly do not know if there is any way to do that. - Smolpi
The answer give will work for you. However, if my calculations are not wrong, you are building a matrix with sparsity of 45%. This matrix, built as a sparse data type, will take more memory that if you were to fill it completely, as a sparse matrix needs to store 3 values for each non-zero, 2 indices and 1 value. - Ander Biguri
I know is not that sparse hahaha, the thing is that I have to implement and algorithm in which I have to update large matrices and I was expecting I could make it faster using matrix operations, otherwise I am afraid I would take days to run the algorithm, I have not even achieved to complete one step on the iterative process... - Smolpi
1) By making the matrix sparse (in this case) you are making the algorithm slower, as it needs more memory than if it were dense. 2) Linear algebra at this scale is not necessarily faster than a loop, just by memory management you lose a significant chunk on time. You seem to have the XY problem, where you are asking about an attempted solution rather than the real problem you have - Ander Biguri

1 Answers

1
votes

This may not be the most efficient method, but you can do this by constructing new vectors which contain your entire list of indice pairs.

W1 = repmat(V1,length(V2),1); %repeat whole vector
W2 = repelem(V2,length(V1)); %repeat each element so it matches with each V1 element

Substitute W1,W2 into your expression for M in place of V1,V2

If you aren't constrained to only store M in sparse format,

M = zeros(L);
M(V1,V2) = 1;

will give the same matrix. (And as @AnderBiguri commented this may actually use less memory in this particular case)