MATLAB: Processing timer for Sparse matrix is too long

large matrixmining dataout of memorytiming process

Dear All,
I am a new user with Matlab, and I meet a problem with Sparse matrix.
Time for processing with Sparse matrix is very long.
Here is my code:
lab=randi([1 7],1,100000);
M = sparse(100000,100000);
for i = 1:100000
for j = (i+1):100000
if lab(i,1) == lab(j,1)
M(j,i) = 1;
else
M(j,i) = 0;
end
end
end
Do I have any method for reduce timing process? Thanks and Best regard!

Best Answer

You won't be able to do it, vectorizing or not.
If random integers are really in 1:7, the probability, when you pick two elements, that one is equal to the other is 1/7. On average, with your lab vector of n=1e6 elements and assuming that you are interested in the lower triangular part of M, this means n^2/2/7 non-zero elements, and hence
>> (16 * (1e6)^2/2/7 + 8 * 1e6 + 8) / 1e12
ans =
1.1429
more than 1.14 TB of RAM to store (just a single version of it).
Are random integers really in 1:7 or is the pool of integers larger, e.g. 1:n ? In the second case, you will gain a lot by vectorizing the approach. Often in these cases that involve sparse matrices, the best approach consists in building indices of non-zero elements ( I and J ), and performing a unique call to SPARSE at the end:
M = sparse( I, J, 1, n, n ) ; % Values are all 1 in your case.