I am using Matlab and I have a sparse vector (only having about 10% of nonzero values but otherwise quite arbitrary).
I want to compress it (smallest size). I also want to know the compression ratio I obtained.
I am using Matlab and I have a sparse vector (only having about 10% of nonzero values but otherwise quite arbitrary).
I want to compress it (smallest size). I also want to know the compression ratio I obtained.
You can get the size of any variable in MATLAB using the whos
function. It returns a struct containing the name, size, class, number of bytes and some other values of a variable. To get information on a variable A
, you call
info = whos('A');
So you could e.g. do the following:
% Create matrices
A = [0 1 0 0; 1 0 0 0; 0 0 1 0; 0 0 0 1]
S = sparse(A)
before = whos('A')
after = whos('S')
comprRatio = before.bytes / after.bytes
which in this small examples returns
comprRatio =
1.2308
as the matrix A
is 128 bytes and the sparse matrix S
is 104 bytes.
If you do any other compression (I did not completely get what sort of compression you are trying to achieve), you can do exactly the same thing with whos
.