I'm trying to export a sparse array from Mathematica to share with collaborators who primarily use Matlab.
The sparse array in question is 4 dimensional, (72 x 93 x 94 x 172) with ~4M non-zero elements. I've tried a number of possibilites but haven't found a working format. Is there a cross-program compatible format for large multidimensional (sparse)matrices?
The data is easily exported in the native Mathematica WDX
and MX
formats, except, as far as I can tell these can't be read by any other programs.
Based on this question/answer (support-for-compressed-sparse-column-sparse-matrix-representation), I've tried the HarwellBoeing
RSA
format.
test = SparseArray[RandomInteger[{1, 100}, {10^6, 4}] -> RandomInteger[10, 10^6]]
Export["text.rsa", test]
This returns: Export::rank:
The sparse array SparseArray[<<1>>]
to be exported in HarwellBoeing
format is not a matrix of rank 2.
I've also tried the MatrixMarket
MTX
format:
Export["text.mtx", test]
This gives a different error:
Export::type:
SparseArray[<<1>>]
cannot be exported to the MatrixMarket
format.
The matlab format, the most obvious choice, also doesn't work:
Export["text.mat", test]
Another new error this time:
Export::type:
{<<1>>}
cannot be exported to the MAT
format.
However, the Matlab MAT
format appears to support 3 dimensional sparse arrays, in this case we can export each 3D array making up the 4D array and it works, although when imported into matlab each file is actually 100 2D arrays, so the 3D actually support isn't implemented correctly and does not give a matlab 3D matrix.
MapThread[Export, {# <> ".mat" & /@ ToString /@ Range[100], test}]