Matrix in a data file

Hi everyone

I have a matrix like that in a data file:

0 1 2
0 3 4
0 5 6
1 7 8
1 9 10
1 11 12
2 13 14
2 15 16
2 17 18

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.

This should be the output using the above matrix:

0 1 2 1 7 8 2 13 14
0 3 4 1 9 10 2 15 16
0 5 6 1 11 12 2 17 18

And this is my code:



#include <iostream>
#include <fstream>

int main()
{

#define row 9
#define col 3

float mat[row][col];

//read data
std::ifstream read ("matrix.dat");

if (!read){
std::cout<<"File doesn't exist";
return 1;
}

for (int r=0; r<row; r++){
for (int c=0; c<col; c++){

read >> mat[r][c];

}}

//write new output
std::ofstream write ("newMat2.dat");

for (int r=0; r<row; r++){
if (mat[r][0]== mat[r+1][0]){
for (int c=0; c<col; c++){

write << mat[r][c] << " ";
}
write <<std::endl;
}}

read.close();
write.close();
return 0;

}

-------------------------------
But this is the output I get :(
:

0 1 2
0 3 4
1 7 8
1 9 10
2 13 14
2 15 16

Would you be able to help? I can't do it manually because the matrix I use can be much bigger.
Thanks a lot!
@jenx,
Your description is not very clear, but your example is suggesting that you want to transform a block matrix
---
A
---
B
---
C
---
.
.
.

into

[A | B | C | ...]

where each of A, B, C, ... is an n x n matrix (n=3 in your example)

Is that correct?
Yes, this is correct. I managed to do that in the end defining a new matrix and a code break, thank you.
Ah well, I'm glad you got it to work.

FWIW this is what I hacked out for the problem
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
{
   const int W = 3;          // width of output field; you may want to increase this

   const int COLS = 3;
   const int ROWS = 3;
   const int 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();
}


matrix.dat
0 1 2
0 3 4
0 5 6
1 7 8
1 9 10
1 11 12
2 13 14
2 15 16
2 17 18


newmatrix.dat
  0     1     2     1     7     8     2    13    14   
  0     3     4     1     9    10     2    15    16   
  0     5     6     1    11    12     2    17    18



If numbers of rows, columns and blocks aren't known until run time then it would probably be better to use std::vector rather than fixed arrays.
Last edited on
Topic archived. No new replies allowed.