How to print submatrix?

I'm trying to plot out this matrix :
1
2
3
4
5
6
7
11 12 13 14  0  0 
0  22 23  0  0  0 
0  0  33 34 35 36 
0  0  0  44 45  0 
0  0  0   0  0 56 
0  0  0   0  0 66 


in this way :

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
11 12
0  22

13 14
23  0

0  0 
0  0 

0  0
0  0 

23  0
33 34

35 36
45  0

0  0
0  0

0  0
0  0

0 56 
0 66


but I'm not figure out how to do !

with this code i got :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for(auto k=0; k < bsz ; k++)
      {
        for(auto i=k; i < sz+k ; i++ )
        {
           for(auto j=k ; j < sz+k ; j++)
           {
               {
                  std::cout << dense[i][j] << ' ' ;
               }   
           }
           std::cout << std::endl;
       }
    std::cout << std::endl;  
} 


I got this wrong result:

1
2
3
4
5
6
7
8
9
10
11
11 12 
0 22 

22 23 
0 33 

33 34 
0 44 

44 45 
0 0 


Could you help me ? thanks in advance

I
Last edited on
I solved it :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
for(auto w=0; w<= bsz ; w+=2)
     {
      for(auto k=0; k <= bsz ; k+=2)
      {
        for(auto i=w; i < sz+w ; i++ )
        {
           for(auto j=k ; j < sz+k ; j++)
           {
               {
                  std::cout << dense[i][j] << ' ' ;
               }   
           }
           std::cout<< std::endl;
       }
       std::cout << std::endl;
      } 
    }
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
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;

using matrix = vector< vector<int> >;


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


void print( const matrix &A )
{
   #define SP << setw( 2 ) << right <<

   int cols = A[0].size();
   int rows = A.size();
   for ( int i = 0; i < rows; i++ )
   {
      for ( int j = 0; j < cols; j++ ) cout SP A[i][j] << ' ';
      cout << endl;
   }
}


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


matrix submatrix( const matrix &A, int I, int J, int rows, int cols )
{
   matrix result( rows, vector<int>( cols ) );
   for ( int i = 0; i < rows; i++ )
   {
      for ( int j = 0; j < cols; j++ ) result[i][j] = A[I+i][J+j];
   }
   return result;
}


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


int main()
{
   const int STEPS = 2;
   matrix A = { { 11, 12, 13, 14,  0,   0 },
                {  0, 22, 23,  0,  0,   0 },
                {  0,  0, 33, 34, 35,  36 },
                {  0,  0,  0, 44, 45,   0 },
                {  0,  0,  0,  0,  0,  56 },
                {  0,  0,  0,  0,  0,  66 } };

   int COLS = A[0].size();
   int ROWS = A.size();

   for ( int i = 0; i < ROWS; i += STEPS )
   {
      for ( int j = 0; j < COLS; j += STEPS )
      {
         print( submatrix( A, i, j, STEPS, STEPS ) );
         cout << endl;
      }
   }
}


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


11 12 
 0 22 

13 14 
23  0 

 0  0 
 0  0 

 0  0 
 0  0 

33 34 
 0 44 

35 36 
45  0 

 0  0 
 0  0 

 0  0 
 0  0 

 0 56 
 0 66 
Last edited on
Topic archived. No new replies allowed.