How to assign values to a sparse matrix without iterations?
Show older comments
Hi, All,
I want to assign non-zero values to a matrix H. I know the locations of all the non-zeros. For example, I know the non-zero locations:

It indicates H(1,1)=1;H(1,2)=1;H(2,1)=1;H(2,5)=1;H(3,2)=1;H(3,3)=1;...
Would you please tell me how I can quickly assign this values without iterations?
Thanks a lot. Bei
Accepted Answer
More Answers (1)
dpb
on 9 Oct 2018
S=sparse(Nonloc(:,1),Nonloc(:,2),1);
6 Comments
Benson Gou
on 10 Oct 2018
Well, I guess that depends on what is "correct". It sets the index values in the Nonloc array to 1 for each i,j pair by row (what I intended and inferred was what Nonloc was representing).
I see looking at the example you've got some other arrangement listed; I don't follow what the logic is that selected those locations???
Bruno Luong
on 10 Oct 2018
The logic is adam code:
- i is the number of rows of NonLoc,
- j are 2 values of the row #i of NonLoc.
dpb
on 10 Oct 2018
If you say so...I still don't see it, but I guess if he does that's all that matters.
Benson Gou
on 11 Oct 2018
dpb
on 11 Oct 2018
Ah! The light finally dawneth...the row isn't given but implied.
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!