I would like to transform it and write it in another data file. Basically I'd like to use the first column as an index. If the index is 0, the three entries stay as they are. But everytime the index changes, the three entries are moved next to column 3. So, i.e., if index is 0, the three entries would be in columns 1-2-3. If it is 1, they are moved to columns 4-5-6. If index is 2 they are moved to columns 7-8-9, and so on for bigger files.
#include <iostream>
#include <iomanip>
#include <fstream>
usingnamespace std;
int main()
{
constint W = 3; // width of output field; you may want to increase this
constint COLS = 3;
constint ROWS = 3;
constint BLOCKS = 3; // I have a suspicion this may be bigger
double M [BLOCKS*ROWS][COLS];
double MT[ROWS][BLOCKS*COLS];
// Read M from file (you'd better be right about dimensions!)
ifstream fin( "matrix.dat" );
for ( int i = 0; i < BLOCKS * ROWS; i++ )
{
for ( int j = 0; j < COLS; j++ ) fin >> M[i][j];
}
fin.close();
// Compose MT, block by block
for ( int b = 0; b < BLOCKS; b++ )
{
int istart = b * ROWS;
int jstart = b * COLS;
for ( int i = 0; i < ROWS; i++ )
{
for ( int j = 0; j < COLS; j++ ) MT[i][jstart+j] = M[i+istart][j];
}
}
// Write MT to file
ofstream fout( "newmatrix.dat" );
for ( int i = 0; i < ROWS; i++ )
{
for ( int j = 0; j < BLOCKS * COLS; j++ ) fout << setw( W ) << MT[i][j] << " ";
fout << '\n';
}
fout.close();
}