9
$\begingroup$

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}]
$\endgroup$

1 Answer 1

7
$\begingroup$

If exporting the SparseArray does not work directly, you could perhaps use

sa=SparseArray[;;;]

sa["NonzeroPositions"]
sa["NonzeroValues"]

and then reconstruct the SparseArray in the other tool. I understand that this is most likely not ideal but it could work. Also, someone else might know better, I am not sure the RSA can deal with rank > 2 matrices.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.