Creating a sparse vector inside a for loop.
7 次查看(过去 30 天)
显示 更早的评论
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."
回答(2 个)
Amit
2014-1-21
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 个评论
Amit
2014-1-21
if N is between 2000 and 4000, dont use sparse. simply initiate v as
v = zeros(N,1);
Amit
2014-1-22
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.
Matt J
2014-1-21
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 个评论
Matt J
2014-1-22
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!