How to Export/Write a SparseArray?
How to Import/Read a SparseArray?
Is it always stored as a normal array?
How can it be stored in a dense form?
Is a numerical SparseArray different to
Export/Import from a string one?
- 12$\begingroup$ What a nice poem $\endgroup$Rojo– Rojo2013-05-14 09:27:27 +00:00Commented May 14, 2013 at 9:27
- $\begingroup$ You can use the methods described in answer to this question: mathematica.stackexchange.com/q/1959/121 In fact I am inclined to close this question as I believe it has already been answered. $\endgroup$Mr.Wizard– Mr.Wizard2013-05-14 11:08:58 +00:00Commented May 14, 2013 at 11:08
1 Answer
The two file formats I am familiar with, with respect to sparse matrices, are the Harwell-Boeing format, and the Matrix Market format. I have linked to the docs on how you can Import[]/Export[] matrices in those formats, and you'll do well by reading up on them.
Sparse arrays are very much different from lists. For instance, consider these two representations of the $9\times 9$ identity matrix:
idDense = IdentityMatrix[9]; idSparse = SparseArray[Band[{1, 1}] -> 1, {9, 9}]; For the purposes of Equal[], they're the same:
idSparse == idDense True SameQ[] shows that they aren't:
idSparse === idDense False One can peek at heads to see the difference:
Head /@ {idSparse, idDense} {SparseArray, List} If, for some reason, you need to convert your sparse array to a dense array, that's what Normal[] is for:
Normal[idSparse] === idDense True See the docs for SparseArray[] and Normal[] for more details.
- $\begingroup$ OK. . .What if you have txt char data instead? . .As in your example idSparse above replace the 1 by "A"? . .It is still a SparseArry. . .How to export this one? . .Can't see it in Harwell-Boeing or Matrix Market. $\endgroup$Hp Radojewski Schäfer Von– Hp Radojewski Schäfer Von2013-05-15 14:04:34 +00:00Commented May 15, 2013 at 14:04
- $\begingroup$ I don't believe any of those formats will work for data with text entries. You will have to use
Normal[]on your sparse array and then export to your favorite format. $\endgroup$J. M.'s missing motivation– J. M.'s missing motivation2013-05-15 14:07:50 +00:00Commented May 15, 2013 at 14:07