I am trying to create a table which when exported in a .csv format and opened in MS.Excel has the following form;
i1 j1 f[i1,j1]
i1 j2 f[i1,j2]
i1 j3 f[i1,j3]
i2 j1 f[i2,j1]
i2 j2 f[i2,j2]
i2 j3 f[i2,j3]
i3 j1 f[i3,j1]
i3 j2 f[i3,j2]
i3 j3 f[i3,j3]
...
I have tried;
myTable = Table[{i, j, f[i, j]}, {i, 1, 3}, {j, 1, 3}]
Export["out.csv", myTable]
but it comes out as;
{{{1, 1, f[1, 1]}, {1, 2, f[1, 2]}, {1, 3, f[1, 3]}},
{{2, 1, f[2, 1]}, {2, 2, f[2, 2]}, {2, 3, f[2, 3]}},
{{3, 1, f[3, 1]}, {3, 2, f[3, 2]}, {3, 3, f[3, 3]}}}
Which in MS.Excel looks like;
{i1 j1 f[i1,j1]} {i1 j2 f[i1,j2]} {i1 j3 f[i1,j3]}
{i2 j1 f[i2,j1]} {i2 j2 f[i2,j2]} {i2 j3 f[i2,j3]}
{i3 j1 f[i3,j1]} {i3 j2 f[i3,j2]} {i3 j3 f[i3,j3]}
The 3 sets of nested brackets means it doesn't come out how I wanted it.
I am sure this is a really simple/dumb question but I am a bit lost here.
Answer
I am not sure if you want numbers or functions. Since you are using comma separated variables I have converted your f[i,j] to a string to avoid the comma in the function creating an extra column.
myTable = Table[{i, j, ToString[f[i, j]]}, {i, 1, 3}, {j, 1, 3}]
If you look at the output there is a level of bracketing associated with each table index. You don't want this so we can remove it with flatten
flatTable = Flatten[myTable, 1]
Now use TableForm to check the configuration
TableForm[flatTable]
The output is

Now you can export
Export["out.csv", flatTable]
Comments
Post a Comment