How to edit sparse matrix entries inside mex file?

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

To be a little bit more specific - I only want to edit the diagonal terms - so a method that doesn't involve looping through all the entries would be better...
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.
I'm certain that any elements that I will edit are already non-zero.
OK. I will work up a short example and post it later on.
Thank you very much for your help! One way I was thinking of doing it (It's not exactly elegant), is to write a matlab function that changes the (i,j)th element of an array, given the array, and the indices. Then use mexCallMATLAB to call this function.
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.
Yes, I found that out the hard way. Also the whole thing was really expensive (and wrong). I've now implemented a new way where I use the IR and JC matrices to arrive at the diagonals and edit the entry in the corresponding pr matrix.

댓글을 달려면 로그인하십시오.

답변 (1개)

Steven Lord
Steven Lord 2016년 1월 8일

1 개 추천

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

Thank you, I will look into this. I'm fairly certain that my modifications don't alter the structure of the sparse matrix, only the non-zero entries.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

질문:

2016년 1월 8일

댓글:

2016년 1월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by