title: Print a matrix in a text file
body: <p>I want to plot a 3D plot (x,y,z) with gnuplot. </p>
  
  <p>In order to do so I thought of writing a matrix to a textfile using fstream in C++ and then according to <a href="https://stackoverflow.com/questions/12750005/gnuplot-3d-plot-of-a-matrix-of-data">this post</a> get the 3D plot of a matrix of data using splot.  </p>
  
  <p>Assuming that this is the right way to do that the data inside the text file should look like this:</p>
  
  <pre><code>       x[1]    x[2]      x[3] 
  y[1] z[1][1]  z[1][2]   z[1][2]
  y[2] z[2][1]  z[1][2]   z[2][3]
  y[3] z[3][1]  z[3][2]   z[3][3]
  </code></pre>
  
  <p>In order to get the matrix I wrote the following code:</p>
  
  <pre><code>fstream myfile;
  myfile.open("example.txt",fstream::out);
  
  //rows
  for (int j=0; j&lt; 3;j++)
  {
    myfile &lt;&lt; x[j]&lt;&lt; std::endl;
  }
  
  //columns
  for (int i=0; i&lt; 3;i++)
  {
    myfile &lt;&lt; y[i]&lt;&lt; std::endl;
  }
  
  //columns
  for (int i=1; i&lt; 3;i++)
  {
    //rows
    for (int j=1; j&lt; 3;j++)
    {
      myfile &lt;&lt; z[i][j] &lt;&lt; std::endl;
    }
  }
  
  myfile.close();
  </code></pre>
  
  <p>I get everything in a column this way, so the question is how can I print a matrix?</p>