create matrix with 2D vector and transpose it

How I can implement the following matrix by 2D vector in C++?
Last edited on
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;

using matrix = vector< vector<int> >;


//======================================================================


matrix transpose( const matrix &M )
{
   int rows = M.size();
   int cols = M[0].size();

   matrix T( cols, vector<int>( rows ) );

   for ( int i = 0; i < rows; i++ )
   {
      for ( int j = 0; j < cols; j++ ) T[j][i] = M[i][j];
   }

   return T;
}


//======================================================================


void write( const matrix &M )
{
   const int w = 1;
   for ( auto row : M )
   {
      for ( auto e : row ) cout << setw( w ) << e << ' ';
      cout << '\n';
   }
}


//======================================================================


int main()
{
   matrix M = { { 0, 0, 0, 1, 1, 1, 1 },
                { 0, 1, 1, 0, 0, 1, 1 },
                { 1, 0, 1, 0, 1, 0, 1 } };
   matrix T = transpose( M );


   cout << "Original matrix:\n";
   write( M );
   cout << "\n\n";
   cout << "Transposed matrix:\n";
   write( T );
}



Original matrix:
0 0 0 1 1 1 1 
0 1 1 0 0 1 1 
1 0 1 0 1 0 1 


Transposed matrix:
0 0 1 
0 1 0 
0 1 1 
1 0 0 
1 0 1 
1 1 0 
1 1 1 
@lastchance
could you please apply transpose matrix in the following code?
Last edited on
Mk87 wrote:
@lastchance
could you please apply transpose matrix in the following code?


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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>         // PLEASE INCLUDE YOUR HEADERS
#include <iomanip>
#include <vector>
using namespace std;

using matrix = vector< vector<int> >;

//============= ADDED ==================================================


matrix transpose( const matrix &M )
{
   int rows = M.size();
   int cols = M[0].size();

   matrix T( cols, vector<int>( rows ) );

   for ( int i = 0; i < rows; i++ )
   {
      for ( int j = 0; j < cols; j++ ) T[j][i] = M[i][j];
   }

   return T;
}



void write( const matrix &M )
{
   const int w = 1;
   for ( auto row : M )
   {
      for ( auto e : row ) cout << setw( w ) << e << ' ';
      cout << '\n';
   }
}


//=========== END OF ADDED =============================================


matrix checkMatrix(int N)
{
        int size = 1 << N;                               
        matrix H(N, vector<int>(size));
        for (int j = 0; j < size; j++)
        {
                for (int i = 0; i < N; i++)
                {
                        H[N - 1 - i][j] = bool(j & (1 << i));
                }
        }
        return H;
}



int main()
{
        const int N = 4;


        matrix H = checkMatrix(N);
        cout << "Matrix H:\n";
        write(H);

        cout << "\n\n";

        matrix T = transpose( H );               // ADDED
        cout << "Matrix T:\n";                   //
        write(T);                                //

//      int pause;
//      std::cin >> pause;
}


Matrix H:
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 
0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 
0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 


Matrix T:
0 0 0 0 
0 0 0 1 
0 0 1 0 
0 0 1 1 
0 1 0 0 
0 1 0 1 
0 1 1 0 
0 1 1 1 
1 0 0 0 
1 0 0 1 
1 0 1 0 
1 0 1 1 
1 1 0 0 
1 1 0 1 
1 1 1 0 
1 1 1 1 
@lastchance

Thank you, it works well, but in a code that I sent above in line 25 with manipulating the value of j I could eliminate the first column of matrix H, but in your last code I cannot eliminate the first column of matrix H and the first row of matrix T that contain elements zero.

Last edited on
@lastchance

Thank you,
Last edited on
Just change your checkMatrix routine to
1
2
3
4
5
6
7
8
9
10
11
12
13
14
matrix checkMatrix(int N)
{
        int FIRST = 1;          // to start from 1 rather than 0
        int size = 1 << N;                               
        matrix H(N, vector<int>(size-FIRST));
        for (int j = FIRST; j < size; j++)
        {
                for (int i = 0; i < N; i++)
                {
                        H[N - 1 - i][j-FIRST] = bool(j & (1 << i));
                }
        }
        return H;
}


If you ever want your column of 0's back then you can just reset FIRST to 0.
@lastchance

In my project, I have a base class and 2 derived classes which this code is a part of derived classes,

Last edited on
Mmm, yes. But (in answering your original question) I used built-in features: specifically a vector<vector<int>> for a matrix.

If you want a separate matrix class (or any other class - I don't quite follow what you are doing) then you are going to have to rewrite these functions to be consistent with your class. I'm afraid that I can't keep up with the changing requirements.

I would suggest that
void TransposedMatrix(const matrix &H)
is never going to work. Either your transposed matrix is going to have to be the return value of the function, or it will change through the function parameter (which couldn't then be const).

Sorry @MK87, but I think it should be you rewriting these routines if you want to use other than the built-in features.

Many of the textbooks on C++ develop either a string class or a matrix class as their example of a fully-fledged class. Transposing is a realtively standard feature of the latter.
Last edited on
@lastchance
if I have a vector instead of const/matrix my problem will solve
Last edited on
Sorry @Mk87, but you keep changing your requirements.

I've given enough code for you to transpose a matrix. If you want to change the container then you are welcome to, but it is your task.

Or anybody else in the forum is welcome to take over.
Topic archived. No new replies allowed.