Processing timer for Sparse matrix is too long?
이전 댓글 표시
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!
댓글 수: 1
Tim Berk
2017년 9월 19일
Dear Nghia,
Looping through large arrays is very slow, especially in what is called 'nested loops' as you are using. Your nested loop requires 100000*100000/2 calculations (the 1/2 because of the (i+1) as starting value for j).
Lets assume that evaluating
if lab(1,i) == lab(1,j)
M(i,j) = 1;
else M(j,i) = 0;
end
Takes 10 micro seconds (which I think is quite a low estimate), than your script would take 1E5*1E5*0.5*1E-5 = 50000 seconds (14 hours).
Hope this clarifies why it is slow.
Often this can be solved by vectorization as explained here: https://uk.mathworks.com/help/matlab/matlab_prog/vectorization.html
Hope this helps.
Cheers,
Tim
채택된 답변
추가 답변 (1개)
Walter Roberson
2017년 9월 19일
편집: Walter Roberson
2017년 9월 19일
Doing M(j,i) = 0 is a waste of time on a sparse array unless M(j,i) might already have a non-zero value. sparse() initializes to 0.
Your line,
M = sparse(100000,100000);
is equivalent to
M = sparse([], [], [], 100000, 100000, 0)
which generates a sparse array that is expected to have 0 occupied cells in it. Every time a cell is written, the sparse array needs to be recreated to allocate room for the new entry. It is a lot more efficient to use
M = spalloc(100000, 100000, N)
where N is an estimate of the number of locations that will eventually be occupied with non-zero values. In your situation you can estimate N as 100000^2/7 which is roughly 1428571429. One element in seven occupancy is not very sparse.
Your inner loop could be vectorized, which could improve performance a lot.
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!