Replace elements of sparse matrix from a full matrix.
显示 更早的评论
Dear Experts
I have a huge matrix, M with zeros. I am converting M into sparse using sparse(M). After, I want to replace few positions (idx) of M from other matrix, N. I am using the following: M(idx) = N(idx). But, this is very very slow. Also MATLAB warns: This sparse indexing expression is slow. How I can do the above task?
Thanks in advance.
回答(1 个)
Jos (10584)
2016-3-4
0 个投票
What don't you replace the elements before converting it?
8 个评论
KSSV
2016-3-4
Jos (10584)
2016-3-4
I don't get it. You do start off with a full matrix M which you convert using sparse, don't you?
KSSV
2016-3-5
Walter Roberson
2016-3-5
If you know you are replacing zeros, then you can sparse() up the values that are going in as a second sparse matrix, and add that to the first sparse matrix.
KSSV
2016-3-5
Walter Roberson
2016-3-5
When you create your original sparse matrix, be sure to allow extra nzmax; see http://www.mathworks.com/help/matlab/ref/spalloc.html
KSSV
2016-3-5
Walter Roberson
2016-3-5
Then spalloc() would be appropriate. If you create a matrix without using it or equivalent then you end up doing a lot of sparse reallocations as you add in new values.
类别
在 帮助中心 和 File Exchange 中查找有关 Sparse Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!