tags:

views:

53

answers:

1

I have some matrix which I want to cycle through blocks, the matrix could be of many different sizes, but I can know the size, is there a way to fast cycle through blocks? i.e: to fast output the indexes of the blocks, suppose a matrix of 4*4 I should have:

Block1: (0,0),(0,1)(1,0)(1,1)
Block2: (0,2),(0,3)(1,2)(1,3)
Block3: (2,0),(2,1)(3,0)(3,1)
Block4: (2,2),(2,3)(3,2)(3,3)
Where the indexes are (row,col).

For blocks I mean a submatrix of size sqrt(matrixSize)* sqrt(matrixSize) where matrix is a matrix of matrixSize*matrixSize. For example a matrix of 4*4 has 4 blocks of 2*2, a 9*9 has 9 blocks of 3*3...

I'm workdeing in C, but I think that the pseudocode is useful also, I only need the loop on the indexes... Thanks

A: 

I managed to do it, it's coded in c ...
The variable 'matrix_size' contains the number of rows(or columns) of the matrix.
The variable 'block' contains the precomputed size of one block of the matrix i.e: the sqrt of matrix_size.


for(int i = 0; i< matrix_size; i++)
{
  fprintf(stdout, "Block %u: ", i);
  for(int k= ((i/block)*block) ; k < (((i/block)*block) + block) ;k++)
  {
    for(int j = ((i%block)*block) ; j< (((i%block)*block) + block); j++)
    {
      fprintf(stdout,"(%u,%u)",k,j);
    }
  }
  fprintf(stdout, "\n");

}

luiss