# Exporting mixed data of matrices and vectors / lists

thank you for reading my question.

I have a problem trying to export mixed data from Mathematica. I have different matrices and vectors, which should be combined to a output file.

Here a minimal-working-example:

a = {{1}, {2}, {3}};
b = {{4}, {5}, {6}};
c = {7, 8, 9};
d = Transpose[{a, b, c}];
Export["test.txt", d, "TSV"]


The output file is:

{1} {4} 7
{2} {5} 8
{3} {6} 9


I would like to have the output like this, similar to "//TableForm":

1 4 7
2 5 8
3 6 9


Unfortunately I have no idea how I may achive this. Changing the format, e.g. to Table or CSV did not change anything. How may I export the data correctly?

• d = Transpose[{Flatten@a, Flatten@b, c}];?
– Öskå
Oct 29, 2014 at 11:12
• Yes, thank you very much! Like always, if you know the answer its trivial. I'll write a answer to my question.
– Jan
Oct 29, 2014 at 11:55

The lists "a" and "b" are nested lists. With the Flatten option, the nested lists my be flattened and the nesting removed.

The answer is, as posted from Öskå, to use the Flatten[]-command before the Transpose[]-command is executed.

If the lists are all the same length

Transpose[Flatten /@ {a, b, c}]


But beware that for ragged lists (i.e., those with sub-lists that are not all the same length) Transpose will not work and you have to use Flatten again.

a = {{0}, {1}, {{2}}, {3}};
b = {{4}, {5}, {6}};
c = {7, 8, 9};

Flatten[Flatten /@ {a, b, c}, {2}]


Transpose::nmtx: The first two levels of {{0,1,2,3},{4,5,6},{7,8,9}} cannot be transposed. >>

Flatten[Flatten /@ {a, b, c}, {2}]

{{0, 4, 7}, {1, 5, 8}, {2, 6, 9}, {3}}


As explained here