Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How do I use a sparse matrix that was returned by a mex file?

Subject: How do I use a sparse matrix that was returned by a mex file?

From: Alejandro

Date: 9 Mar, 2013 13:14:10

Message: 1 of 8

Hi all,

Say I created a sparse matrix (say K) in a mex file and returned to matlab environment. I read that sparse matrix has no information of the sparsity and cannot be used in any other matlab function that uses a sparse matrix. So, how can a I use that sparse matrix within matlab environment? I would like to use it to solve a linear system like this:

u = K\f

where u and f are dense vectors. Can I do this using the returned sparse K from the mex file?

Thanks,
Alejandro.

Subject: How do I use a sparse matrix that was returned by a mex file?

From: James Tursa

Date: 9 Mar, 2013 18:09:06

Message: 2 of 8

"Alejandro " <aortizb@ing.uchile.cl> wrote in message <khfcj2$ij1$1@newscl01ah.mathworks.com>...
> Hi all,
>
> Say I created a sparse matrix (say K) in a mex file and returned to matlab environment. I read that sparse matrix has no information of the sparsity and cannot be used in any other matlab function that uses a sparse matrix.

Wrong. If the sparse matrix was created correctly in the mex routine, you can use it just like any other sparse matrix that was created at the m-file level. You can use the FEX function spok to see if it was built properly:

http://www.mathworks.com/matlabcentral/fileexchange/20186-spok-checks-if-a-matlab-sparse-matrix-is-ok


James Tursa

Subject: How do I use a sparse matrix that was returned by a mex file?

From: Alejandro

Date: 9 Mar, 2013 19:56:05

Message: 3 of 8

"James Tursa" wrote in message <khfts1$3qb$1@newscl01ah.mathworks.com>...
> "Alejandro " <aortizb@ing.uchile.cl> wrote in message <khfcj2$ij1$1@newscl01ah.mathworks.com>...
> > Hi all,
> >
> > Say I created a sparse matrix (say K) in a mex file and returned to matlab environment. I read that sparse matrix has no information of the sparsity and cannot be used in any other matlab function that uses a sparse matrix.
>
> Wrong. If the sparse matrix was created correctly in the mex routine, you can use it just like any other sparse matrix that was created at the m-file level. You can use the FEX function spok to see if it was built properly:
>
> http://www.mathworks.com/matlabcentral/fileexchange/20186-spok-checks-if-a-matlab-sparse-matrix-is-ok
>
>
> James Tursa

Thanks James. I read somewhere in the manual what I mentioned above, so that cause me a doubt. Also, I haven't written the function yet. Maybe I misunderstood the manual, so I wanted to be sure I would be able to use the sparse matrix after it was created inside the mex function.

What is not clear to me is the following. If only the sparse matrix that is created inside the mex function is returned to the matlab environment, how does matlab know the internal structure of that sparse matrix? do I have to return something else like ir and jc?

Thanks.

Subject: How do I use a sparse matrix that was returned by a mex file?

From: James Tursa

Date: 9 Mar, 2013 23:40:13

Message: 4 of 8

"Alejandro " <aortizb@ing.uchile.cl> wrote in message <khg44l$jq8$1@newscl01ah.mathworks.com>...
> "James Tursa" wrote in message <khfts1$3qb$1@newscl01ah.mathworks.com>...
> > "Alejandro " <aortizb@ing.uchile.cl> wrote in message <khfcj2$ij1$1@newscl01ah.mathworks.com>...
> > > Hi all,
> > >
> > > Say I created a sparse matrix (say K) in a mex file and returned to matlab environment. I read that sparse matrix has no information of the sparsity and cannot be used in any other matlab function that uses a sparse matrix.
> >
> > Wrong. If the sparse matrix was created correctly in the mex routine, you can use it just like any other sparse matrix that was created at the m-file level. You can use the FEX function spok to see if it was built properly:
> >
> > http://www.mathworks.com/matlabcentral/fileexchange/20186-spok-checks-if-a-matlab-sparse-matrix-is-ok
> >
> >
> > James Tursa
>
> Thanks James. I read somewhere in the manual what I mentioned above, so that cause me a doubt. Also, I haven't written the function yet. Maybe I misunderstood the manual, so I wanted to be sure I would be able to use the sparse matrix after it was created inside the mex function.
>
> What is not clear to me is the following. If only the sparse matrix that is created inside the mex function is returned to the matlab environment, how does matlab know the internal structure of that sparse matrix? do I have to return something else like ir and jc?

Inside the mex function you are required to fill in the pr, pi (if complex), ir, and jc arrays manually. If you do it correctly, the result will be a sparse matrix with the exact internal structure as if it was created at the m-file level.

James Tursa

Subject: How do I use a sparse matrix that was returned by a mex file?

From: Alejandro

Date: 10 Mar, 2013 00:34:09

Message: 5 of 8

> > What is not clear to me is the following. If only the sparse matrix that is created inside the mex function is returned to the matlab environment, how does matlab know the internal structure of that sparse matrix? do I have to return something else like ir and jc?
>
> Inside the mex function you are required to fill in the pr, pi (if complex), ir, and jc arrays manually. If you do it correctly, the result will be a sparse matrix with the exact internal structure as if it was created at the m-file level.
>
> James Tursa


Thank you! Now I am confident with this.

Subject: How do I use a sparse matrix that was returned by a mex file?

From: Alejandro

Date: 10 Mar, 2013 00:38:14

Message: 6 of 8

> > What is not clear to me is the following. If only the sparse matrix that is created inside the mex function is returned to the matlab environment, how does matlab know the internal structure of that sparse matrix? do I have to return something else like ir and jc?
>
> Inside the mex function you are required to fill in the pr, pi (if complex), ir, and jc arrays manually. If you do it correctly, the result will be a sparse matrix with the exact internal structure as if it was created at the m-file level.
>
> James Tursa


Thank you! Now I am confident with this.

Subject: How do I use a sparse matrix that was returned by a mex file?

From: dpb

Date: 10 Mar, 2013 02:36:45

Message: 7 of 8

On 3/9/2013 1:56 PM, Alejandro wrote:
...

> What is not clear to me is the following. If only the sparse matrix that
> is created inside the mex function is returned to the matlab
> environment, how does matlab know the internal structure of that sparse
> matrix? do I have to return something else like ir and jc?

It's part of what you do in the mex function--fill in the internal
structure as well as values. There's an example in both C and Fortran
and links thereto from the documentation...

--

Subject: How do I use a sparse matrix that was returned by a mex file?

From: Steven_Lord

Date: 11 Mar, 2013 02:39:26

Message: 8 of 8



"Alejandro " <aortizb@ing.uchile.cl> wrote in message
news:khfcj2$ij1$1@newscl01ah.mathworks.com...
> Hi all,
>
> Say I created a sparse matrix (say K) in a mex file and returned to matlab
> environment. I read that sparse matrix has no information of the sparsity
> and cannot be used in any other matlab function that uses a sparse matrix.

Where did you read that? If you read that in the documentation, please post
the URL of the page that states that so we can get it corrected.

> So, how can a I use that sparse matrix within matlab environment? I would
> like to use it to solve a linear system like this:
>
> u = K\f
>
> where u and f are dense vectors. Can I do this using the returned sparse K
> from the mex file?

As long as K is a valid sparse matrix, yes. See mxCreateSparse and the
functions linked in the See Also section of that page.

http://www.mathworks.com/help/matlab/apiref/mxcreatesparse.html?searchHighlight=mxCreateSparse

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us