norboot
norboot

Reputation: 5

Matrix of matrices in Java

I need to build a matrix of matrices. For example, in a 3x2 matrix, each position (i, j) must also contain a 2x2 matrix. So, for that case you would have a matrix of 6 spaces and 6 sub-matrices each with 4 spaces. The idea is to fill it and then show it.

int mat [][];
mat = new int[x][y];
*for example* mat[0][0] = new int[2][2]; *this does not allow java*

How can this be resolved?

Upvotes: 0

Views: 103

Answers (4)

Andreas
Andreas

Reputation: 159086

A matrix is a 2D array. A matrix of matrices is therefore a 4D array.

So a 3x2 matrix of 2x2 matrices would be:

int[][][][] matrixOfMatrices = new int[3][2][2][2];

As for how to print it, here is an example:

int outerHeight = 3, outerWidth = 2, innerHeight = 2, innerWidth = 2;
int[][][][] matrixOfMatrices = new int[outerHeight][outerWidth][innerHeight][innerWidth];

// Fill with some data
for (int i = 1, outRow = 0; outRow < outerHeight; outRow++)
    for (int outCol = 0; outCol < outerWidth; outCol++)
        for (int inRow = 0; inRow < innerHeight; inRow++)
            for (int inCol = 0; inCol < innerWidth; inCol++)
                matrixOfMatrices[outRow][outCol][inRow][inCol] = i++;

// Print 2D x 2D matrix
int numWidth = Integer.toString(outerHeight * outerWidth * innerHeight * innerWidth + 1).length();
for (int outRow = 0; outRow < outerHeight; outRow++) {
    if (outRow != 0) {
        for (int outCol = 0; outCol < outerWidth; outCol++) {
            if (outCol != 0)
                System.out.print("-+-");
            System.out.print("-".repeat(innerWidth * (numWidth + 1) - 1));
        }
        System.out.println();
    }
    for (int inRow = 0; inRow < innerHeight; inRow++) {
        for (int outCol = 0; outCol < outerWidth; outCol++) {
            if (outCol != 0)
                System.out.print(" | ");
            for (int inCol = 0; inCol < innerWidth; inCol++) {
                if (inCol != 0)
                    System.out.print(" ");
                System.out.printf("%" + numWidth + "d", matrixOfMatrices[outRow][outCol][inRow][inCol]);
            }
        }
        System.out.println();
    }
}

Output

 1  2 |  5  6
 3  4 |  7  8
------+------
 9 10 | 13 14
11 12 | 15 16
------+------
17 18 | 21 22
19 20 | 23 24

To see printed result of a different size:

int outerHeight = 2, outerWidth = 3, innerHeight = 4, innerWidth = 5;

Output

  1   2   3   4   5 |  21  22  23  24  25 |  41  42  43  44  45
  6   7   8   9  10 |  26  27  28  29  30 |  46  47  48  49  50
 11  12  13  14  15 |  31  32  33  34  35 |  51  52  53  54  55
 16  17  18  19  20 |  36  37  38  39  40 |  56  57  58  59  60
--------------------+---------------------+--------------------
 61  62  63  64  65 |  81  82  83  84  85 | 101 102 103 104 105
 66  67  68  69  70 |  86  87  88  89  90 | 106 107 108 109 110
 71  72  73  74  75 |  91  92  93  94  95 | 111 112 113 114 115
 76  77  78  79  80 |  96  97  98  99 100 | 116 117 118 119 120

Upvotes: 0

Vijay Kumar
Vijay Kumar

Reputation: 89

you can achieve this via the collection list. something like below. the code below creates a list of matrices. this is more maintainable compared to having a multidimensional array.

    List<int[][]> matrixArray = new ArrayList<int[][]>();
    int[][] a1= new int[2][2];
    int[][] a2= new int[2][2];
    int[][] a3= new int[2][2];
    int[][] a4= new int[2][2];
    matrixArray.add(a1);
    matrixArray.add(a2);

Upvotes: -1

Mahamudul Hasan
Mahamudul Hasan

Reputation: 2823

int mat [][][][]=new int[3][2][2][2];

where,

mat[0][0] is 2x2 matrix
mat[0][1] is 2x2 matrix
mat[0][2] is 2x2 matrix

so on...

Upvotes: 2

Jay Dangar
Jay Dangar

Reputation: 3469

You can use Four Dimensional Matrix in Java like following,

int [][][][]fourDimensional = new int[10][10][10][10];

This matrix represents 4 dimensional matrix. It would be useful if you think it like a tensor, which is nothing but a multidimensional array.

Upvotes: 2

Related Questions