Often I need to use same matrix several times and for that I save them in an external file. Converting the matrix into SparseArray
can save a lot of space in this process.
For example consider this SparseArray
.
spdat = Join[RandomInteger[{1, 10}, 2], {RandomReal[]}] & /@ Range[20];
spmat = SparseArray[(#[[1 ;; 2]] -> #[[3]]) & /@ spdat, {10, 10}]
Lets consider this spdat
{{9, 10, 0.11555}, {5, 8, 0.0436915}, {6, 2, 0.376473}, {7, 10, 0.893704}, {6, 3, 0.114267}, {2, 1, 0.860136}, {10, 7, 0.462883}, {5, 5, 0.126532}, {9, 8, 0.327185}, {10, 3, 0.36935}, {1, 8, 0.98363}, {3, 3, 0.864916}, {1, 1, 0.523974}, {1, 8, 0.752075}, {7, 1, 0.0122767}, {10, 4, 0.872767}, {4, 7, 0.555469}, {1, 4, 0.395135}, {7, 8, 0.842624}, {7, 4, 0.943731}}
If I can Export
this as a data file, I can create my SparseArray
from that. A simple Export
will export the whole matrix in dense form which will create a huge file.
If I check the InputForm
of spmat
, it returns,
InputForm[spmat]
SparseArray[Automatic, {10, 10}, 0, {1, {{0, 3, 4, 5, 6, 8, 10, 14, 14, 16, 19}, {{1}, {4}, {8}, {1}, {3}, {7}, {5}, {8}, {2}, {3}, {1}, {4}, {8}, {10}, {8}, {10}, {3}, {4}, {7}}}, {0.5239738483968632, 0.3951345317083965, 0.9836297206953681, 0.8601364789360151, 0.8649158619094384, 0.5554690352192444, 0.12653163124345568, 0.04369153890486932, 0.3764726821240931, 0.11426712799092975, 0.012276677822715243, 0.9437312355928948, 0.8426237324241401, 0.8937036888680452, 0.32718527756860794, 0.11555023823345034, 0.36934965959862076, 0.872767483243448, 0.4628830528072616}}]
How can I retrieve spdat
from this? Definitely the third argument contains the information about the position of nonzero elements, but I can't recognise them!
Export
the sparse array as.m
or.mx
file, then you can simplyImport
it back without thinking what its pieces mean. The.mx
file has an additional advantage that it will keep packed arrays packed. If you do want to understand the anatomy of sparse arrays, you can read e.g. this discussion. $\endgroup$spdat
back you can do something likeMapThread[Append, {#["NonzeroPositions"], #["NonzeroValues"]}] &@spmat
$\endgroup$.mx
format is great, it is not portable across different OS's or across different versions of MMA. I use it all the time, but this is important to keep in mind. $\endgroup$.mx
is portable across OS - but not architectures, and perhaps may not also be across different versions of Mathematica. $\endgroup$.mx
was updated last at V6 and gives the caveats I gave above. $\endgroup$