Creating a sparse vector inside a for loop.

10 views (last 30 days)
Alex
Alex on 21 Jan 2014
Commented: Matt J on 22 Jan 2014
Suppose I have a vector u that does not change. I want to create another vector v = M*u, where the matrix M depends on the index i . For example,
v = sparse(N,1);
for i=1:N
M = makeSomeMatrix(i);
v(i) = (M*u)'*u;
end
Is there a faster way to fill in v? This is the slowest part of my code and it gives me the warning " This sparse indexing expression is likely to be slow."

Answers (2)

Amit
Amit on 21 Jan 2014
The sparse matrix is slow but effective for huge matrixes with a lot of zeros.
but MATLAB can handle big matrixes in normal way quite effectively. In you case, it seems like probably you can work without a sparse matrix. I said that simple because you're looping through N and the matrix v is initiated as Nx1.
  2 Comments
Amit
Amit on 21 Jan 2014
if N is between 2000 and 4000, dont use sparse. simply initiate v as
v = zeros(N,1);
Amit
Amit on 22 Jan 2014
With the new code, I would suggest that sparse is unnecessary. It will be much faster to use normal matrix for such a small matrix size.
You can understand the difference between sparse matrix and a normal matrix. In normal matrix, the whole matrix is stored at a location in memory. So replacing a value of a given index, does not produces significant overhead. Now take it for a sparse matrix. Sparse matrix only stores non-zero value. So, adding a new non-zero matrix over and over again makes MATLAB store the new sparse matrix in a new location which is significant overhead in a loop.

Sign in to comment.


Matt J
Matt J on 21 Jan 2014
Based on what you've shown, M is a row vector (that's the only way the scalar location v(i) can accept the result). You should really construct/concatenate all rows of the matrix in advance and do a single matrix-vector multiplication.
v=M_all*u
It may help if you show us makeSomeMatrix, to see if/why each row needs to be built separately and if/why it requires a for-loop.
  2 Comments
Alex
Alex on 22 Jan 2014
Hi Matt, you're right. That was a mistake. I've changed the example code so that everything is dimensionally correct. The matrix M changes every time and the number (M*u)'*u is stored in an element of the vector v .
Matt J
Matt J on 22 Jan 2014
Alex,
Your change doesn't affect my remarks in any substantive way. You are still generating one row/column (M*u) of a bigger matrix in every iteration of the loop, and the computation of v could be vectorized if you saved and post concatenated them, e.g.,
c=cell(1,N);
for i=1:N
c{i} = makeSomeMatrix(i)*u;
end
v=u.'*[c{:}];
The question of whether you could avoid N calls to makesomeMatrix is still open as well, and will be until we understand what's going on inside that function.

Sign in to comment.

Categories

Find more on Sparse Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!