MATLAB: Query regarding a sparse complex matrix

efficiencysparse

I have a complex matrix, I have to create its sparse form using sparse function. How do I store it in lesser bytes than the original complex matrix? In the first case it is storing in 144 bytes but in the sparse case it is storing in 152 bytes. Any example
A =
0.1620 + 0.8048i 0.3402 + 0.7727i 0.0000 + 0.0000i
0.2497 + 0.1178i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.3937 + 0.6184i 0.4527 + 0.2691i 0.0000 + 0.0000i
>> B = sparse(A)
B =
(1,1) 0.1620 + 0.8048i
(2,1) 0.2497 + 0.1178i
(3,1) 0.3937 + 0.6184i
(1,2) 0.3402 + 0.7727i
(3,2) 0.4527 + 0.2691i
>> whos Name Size Bytes Class Attributes
A 3x3 144 double complex
B 3x3 152 double sparse, complex

Best Answer

Hi,
the resulting size of the sparse matrix depends from the number of zero entries. Consider this:
A = randi(10,100);
A(A<=7) = 0;
B = sparse(A);
whos A B
Name Size Bytes Class Attributes
A 100x100 80000 double
B 100x100 49096 double sparse
The code above produced a sparse matrix which has about 60% size then the normal one. There should be about 70% zeros in it.
If we only have about 30% zeros the situation changes:
A = randi(10,100);
A(A<=3) = 0;
B = sparse(A);
whos A B
Name Size Bytes Class Attributes
A 100x100 80000 double
B 100x100 113240 double sparse
Now the size of B is about 40% bigger than A.
EDIT - Now the same example using complex numbers:
about 70% zeros:
R = randi(10,100);
I = ones(100);
C = complex(R,I);
C(real(C)<=7) = 0;
D = sparse(C);
whos C D
gives:
Name Size Bytes Class Attributes
C 100x100 160000 double complex
D 100x100 71944 double sparse, complex
and the same with about 30% zeros:
R = randi(10,100);
I = ones(100);
C = complex(R,I);
C(real(C)<=3) = 0;
D = sparse(C);
whos C D
gives:
Name Size Bytes Class Attributes
C 100x100 160000 double complex
D 100x100 168928 double sparse, complex
The values differ, but the fundamental effect is the same.
Best regards
Stephan
Related Question