Smolpi
Smolpi

Reputation: 13

Multiple assignment of sparse matrices

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:

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?

Upvotes: 0

Views: 275

Answers (1)

etmuse
etmuse

Reputation: 505

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)

Upvotes: 1

Related Questions