how to do the sparse indexing

6 次查看(过去 30 天)
Jason
Jason 2016-12-14
编辑: Jason 2016-12-14
Hello all, I want to do the following sparse array index. Here, P is a 3-dimansional cell matrix, because the matrix is very large,I cann't use zeros function to define matrix A. But the following code showed 'the sparse indexing expression is likely to be slow.' Do you have any idea to solve that? Thank you !
A=sparse(S,S)
for s=1:S
A(s,:)=P{u(s)}(s,:);
end

回答(1 个)

John D'Errico
John D'Errico 2016-12-14
Hmm. You created A as sparse, and all zero. Then you index into A1, which appears to be undefined. I'll assume that you simply cannot type, and meant A there also.
Instead, you need to learn to actually use sparse, instead of trying to stuff elements into a sparse matrix one at a time. READ THE HELP FOR SPARSE! My re-writing that help for you to read cannot possibly be more efficient than you simply reading the help yourself.
help sparse
doc sparse
As far as improving the code you have written that uses a cell array P of completely unknown shape and size, and some completely unknown function of variable u, you probably need to learn to use tools like cellfun, cat, cell2mat, etc. Then make one call to sparse at the end. But I cannot read your mind about things you won't bother to tell us.

类别

Help CenterFile Exchange 中查找有关 Sparse Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by