Sparse to full matrix conversion not working...

The functions full(.) and issparse(.) are behaving strangely for me. In particular, issparse(.) seems unable to detetct sparse matrices correctly, and full(.) fails to convert sparse matrices to full matrices. For example, when I run the code below:
S = sparse(double(rand(200,200) < 1/2));
SS = speye(200);
A = full(S);
AA = full(SS);
d1=issparse(S)
d2=issparse(SS)
d3=issparse(A)
d4=issparse(AA)
whos
I get the following output:
d1 =
0
d2 =
0
d3 =
0
d4 =
0
Name Size Bytes Class Attributes
A 200x200 321096 double sparse
AA 200x200 4808 double sparse
S 200x200 321096 double sparse
SS 200x200 4808 double sparse
ans 1x1 8 double
d1 1x1 8 double
d2 1x1 8 double
d3 1x1 8 double
d4 1x1 8 double
So, issparse(.) tells me none of the matrices are sparse, while the "whos" command tells me that all four are sparse, when it should be the case that S and SS are sparse, while A and AA are not. Additionally, the Workspace window indicates that all four are sparse.
Any help with why this might be happening would be greatly appreciated. (The reason I need this is because a particular function I'm working with, schur(.), can only take full matrices as arguments.)

2 Comments

A and AA do not show up as sparse for me, and the issparse() tests return correct results for me. R2018b on Mac High Sierra
Thanks for testing it. It seems like it's a problem that, for some reason, is specific to me.

Sign in to comment.

 Accepted Answer

Something is definitely odd here. Your A and AA should be full matrices, not still sparse.
Let's make sure you're calling the correct issparse and full functions.
which -all issparse
which -all full

3 Comments

Thanks for your response. It is odd. After running
which -all issparse
which -all full
output is
C:\ .. \MATLAB\issparse.m % comment: I deleted some of the actual path here
issparse is a built-in method % matlab.lang.OnOffSwitchState method
issparse is a built-in method % targetrepository.physical.Platform method
issparse is a built-in method % targetrepository.toolchain.Endianess method
issparse is a built-in method % targetrepository.toolchain.IntDivRoundTo method
issparse is a built-in method % targetrepository.toolchain.LargestAtomicFloat method
issparse is a built-in method % targetrepository.toolchain.LargestAtomicInteger method
issparse is a built-in method % targetrepository.toolchain.ShiftRightIntArith method
built-in (C:\Program Files\MATLAB\R2018b\toolbox\matlab\sparfun\issparse) % Shadowed
C:\ .. \MATLAB\full.m % comment: I deleted some of the actual path here
full is a built-in method % matlab.lang.OnOffSwitchState method
full is a built-in method % targetrepository.physical.Platform method
full is a built-in method % targetrepository.toolchain.Endianess method
full is a built-in method % targetrepository.toolchain.IntDivRoundTo method
full is a built-in method % targetrepository.toolchain.LargestAtomicFloat method
full is a built-in method % targetrepository.toolchain.LargestAtomicInteger method
full is a built-in method % targetrepository.toolchain.ShiftRightIntArith method
built-in (C:\Program Files\MATLAB\R2018b\toolbox\matlab\sparfun\full) % Shadowed
It does seem like I may have found a workaround by multiplying the sparse matrix by a full identity matrix, but it's still a little troubling that the functions aren't working correctly for me.
C:\ .. \MATLAB\issparse.m % comment: I deleted some of the actual path here
C:\ .. \MATLAB\full.m % comment: I deleted some of the actual path here
Those are likely your culprit. From the tail end of those paths they don't look like the built-in issparse and full functions, but ones you've created or received from someone. Rename those files or delete them and see if you can reproduce the problem.
Thanks! I just figured this out too. Thanks so much for your help! I'm not sure how they got there, but it seems to be working correctly now.

Sign in to comment.

More Answers (0)

Products

Release

R2018b

Community Treasure Hunt

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

Start Hunting!