How to edit sparse matrix entries inside mex file?
Show older comments
Hi all,
I have a large sparse matrix that I pass to a mex file I have written. At some stage in this mex file, I want to update some entries in this large sparse matrix. How do I access specific elements inside this sparse matrix and change them?
Thanks!
Siddharth
7 Comments
SiddharthKrishnamoorthy
on 8 Jan 2016
James Tursa
on 8 Jan 2016
Manipulating sparse matrices in a mex routine can be very tricky. Are the diagonal elements currently non-zero? Are you changing any non-zero elements to zero or vice-versa? If so, you will need to rewrite the entire matrix into a new area of memory (i.e., changes can't be done in-place). Let us know what exactly you are doing and then I can supply some example mex code to do it if it can be done in-place. If it can't be done in-place then the code will be much more involved.
SiddharthKrishnamoorthy
on 8 Jan 2016
James Tursa
on 8 Jan 2016
OK. I will work up a short example and post it later on.
SiddharthKrishnamoorthy
on 8 Jan 2016
James Tursa
on 9 Jan 2016
Note that mexCallMATLAB always returns a deep copy ... it cannot be used for any in-place calculations. So if your intention is to write something that only changes a non-zero value to a different non-zero value quickly in-place, you definitely do not want to use mexCallMATLAB.
SiddharthKrishnamoorthy
on 11 Jan 2016
Answers (1)
Steven Lord
on 8 Jan 2016
1 vote
You will need to manipulate the ir, pr (and pi if the data is complex), and jc. See this documentation page and this other documentation page for a description of the jc and ir pointers and the fulltosparse example on this third documentation page for an example of a similar type of manipulation.
You're also going to want to be very careful that your modifications don't result in an invalid sparse matrix.
1 Comment
SiddharthKrishnamoorthy
on 8 Jan 2016
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) 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!