### How to print submatrix?

I'm trying to plot out this matrix :
 ``1234567`` ``````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 :

 ``123456789101112131415161718192021222324252627`` ``````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 :
 ``1234567891011121314`` ``````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:

 ``1234567891011`` ``````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 :
 ``1234567891011121314151617`` ``````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
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667`` ``````#include #include #include using namespace std; using matrix = vector< vector >; //===================================================================== 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( 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.