1

I want to traverse a 3*3 submatrix within a large 7*7 matrix starting position from (1,1) that is middle element (2nd row , 2nd column). The corresponding submatrix of position (1,1) will be

                [(0,1),(0,2),(0,3)]
                [(1,1),(1,2),(1,3)]
                [(2,1),(2,2),(2,3)]

Like this traversing will go on.. and next submatrix starting posiion will be (1,2)

                 [(0,2),(0,3),(0,4)]
                 [(1,2),(1,3),(1,4)]
                 [(2,2),(2,3),(2,4)]

My Code

static int i;
static int j;
static int g;
static int h;

static void submatrix(int p,int q,int[][] mat) {

System.out.print("Submatrix for : ");
System.out.println(p+","+q);
shiftmatrix(p,q,mat);
}

static void shiftmatrix(int p,int q,int[][] mat) {
 int m,n;
 int[][] d = new int[3][3];
 for( m=0;m<3;m++) {
  for( n=0;n<3;n++) {
   p=m+(p-1);
   q=n+q;
   d[m][n]=mat[p][q];
     }
   }

System.out.println("Your 3*3 SubMatrix is : ");
    for ( m = 0; m < 3; m++){
    for ( n = 0; n < 3; n++){
        System.out.print(d[m][n]+"\t");
        }
         System.out.println();
        }
}
public static void main(String[] args) {

    int[][] a = new int[7][7];
    int[][] mat = new int[7][7];
    for ( i = 0; i < 7; i++)
      {
       for ( j = 0; j < 7; j++){
          Random rand = new Random();
           a[i][j] = rand.nextInt(10);
          }
      }

//copying large matrix to another for passing by argument 

 System.out.println("Copied matrix is : ");
 for (i = 0; i < 7; i++){
     for (j = 0; j < 7; j++){
          mat[g][h]=a[i][j];
            System.out.print(mat[g][h]+"\t");
        }
          System.out.println();
    }

//Here is the 3*3 submatrix traversing starts...

 for (i=1;i<6;i++) {
   for (j=1;j<5;j++) {
    int p=i;
     int q=j;
     submatrix(p,q,mat);
     }
  }
}
}

while running this code getting error as

ArrayIndexOutOfBoundsException: -1 

Please help

S.Podder
  • 35
  • 4
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) – lexicore Aug 31 '19 at 07:08

1 Answers1

0

The IndexOutOfBoundsException in your code was from you calling p = m + (p - 1). You don't need to change the p and q variables within every iteration of the loop.

In addition, you had several unnecessary variables, and had some of them static, something you should avoid when you're only using them in a loop like this. After cleaning up the code's formatting and deleting all unnecessary variables I believe the code functions as you want it to.

The code ignores the first row and column of your random matrix. Is this desired behavior?

import java.util.Random;

public class MatrixTest {

    public static void subMatrix(int startRow, int startCol, int[][] mat) {

        System.out.print("Submatrix for : ");
        System.out.println(startRow + ", " + startCol);
        shiftMatrix(startRow, startCol, mat);
    }

    public static void shiftMatrix(int startRow, int startCol, int[][] mat) {
        int[][] d = new int[3][3];
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                //to properly move within the 3x3 you only need to add a 
                //constant buffer to the indices of mat[][]
                d[i][j] = mat[i + startRow][j + startCol]; 
            }
        }
        System.out.println("Your 3*3 SubMatrix is : ");
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.print(d[i][j] + "\t");
            }
            System.out.println();
        }
    }
    public static void main(String[] args) {

        int[][] mat = new int[7][7];
        for (int i = 0; i < 7; i++) {
            for (int j = 0; j < 7; j++){
                Random rand = new Random();
                mat[i][j] = rand.nextInt(10);
            }
        }

        //copying large matrix to another for passing by argument 

        System.out.println("Copied matrix is : ");
        for (int i = 0; i < 7; i++){
            for (int j = 0; j < 7; j++) {
                System.out.print(mat[i][j] + "\t");
            }
            System.out.println();
        }
        //Here is the 3*3 submatrix traversing starts...

        for (int i = 1; i < 5; i++) { //changed from i < 6 to i < 5 to stay inside 7x7
            for (int j = 1; j < 5; j++) {
                subMatrix(i, j, mat);
            }
        }
    }
}
Mihir Kekkar
  • 528
  • 3
  • 11