1
$\begingroup$

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?

Thank you in advance for answering my question.

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

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.

$\endgroup$
0
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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