# Matrix Program in Java In this article, we will learn about what the matrix is, how to define a matrix in java, a simple matrix program in java, the addition of the two matrices, the subtraction of the two matrices, and the multiplication of the two matrices.

## What is a Matrix?

In simple words, the matrix is a two-dimensional (2-D) array. The rectangular-shaped matrix can contain any data type like numbers, characters, strings, symbols, or expressions. In the above, we have taken one matrix for example. The size of the matrix is n*m where n is the number of rows and m is the number of columns. Let’s see how we can access an element from the matrix. From the above picture, if we want to access the first element of the first row we can use a  where a is the name of an array. Now, if we want to access the element from any row or column we can use a[i][j] where i is the row number and j is the column number. Let’s try to write a simple matrix program in java.

## Simple Matrix Program in Java:

Now, we will try to create a simple matrix program in java. In which we will print the matrix and access the matrix element.

```// matrix program in java
class HelloWorld {
public static void main(String[] args) {

// 2D natrix
int a[][]={
{5,6,2},
{9,7,1},
{8,3,1}
};
// the size of the matrix
// n = total numbers of rows
// m = total numbers of columns
int n=a.length;
int m=a.length;

// print the matrix
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}

System.out.println("Element at i=2 and j=1 position: "+a);
}
}```

Output:

``````5 6 2
9 7 1
8 3 1
Element at i=2 and j=1 position: 3``````

In the above program, first, we created a 3*3 size matrix with random integers. After that, we find the size of a matrix using the length method. Now, we will print the matrix using two loops. The outer loop is to iterate through rows. The inner loop is to iterate through the columns.

### Addition Matrix Program in Java:

The addition of the two matrices is possible only when both matrices have the same size. For example, if matrix A is having size 45 and matrix B is having size 45 then only the addition is possible. Now, if matrix A is having size 45 and matrix B is having size 54 then the addition is not possible. In the above example, we have taken two matrices with random integer values. In the image, we can see that the addition is performed on each element with its corresponding element of another matrix. In the result, we can see that elements of both the matrix summed up with their respective position in the matrix. Let’s see how to write an addition matrix program in java.

```// addition matrix program in java
class HelloWorld {
public static void main(String[] args) {

// 2D natrix
int a[][]={
{5,6,2},
{9,7,1},
{8,3,1}
};
int b[][]={
{2,4,1},
{7,6,4},
{9,2,5}
};

int n=a.length;
int m=a.length;

// matrix to store the result
int result[][]=new int[n][m];

for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
result[i][j]=a[i][j]+b[i][j];
}
}

for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
System.out.print(result[i][j]+" ");
}
System.out.println();
}
}
}```

Output:

``````Matrix after addition:
7 10 3
16 13 5
17 5 6 ``````

In the above program, we created two matrices of the same size with random integers. After that, we have taken a matrix result to store the answer. We used two loops to perform the addition of the matrices and stored the answer in the result matrix. In the output, we can see that the result matrix is the addition of the two matrices.

### Subtraction Matrix Program in Java:

The subtraction of the two matrices is possible only when both matrices have the same size. For example, If we want to subtract matrix B from matrix A then we can subtract each element of matrix B from the same positioned element of matrix A. In the above example, we have taken two matrices with random integer values. In the image, we can see that the subtraction performed on each element with its corresponding element of another matrix. In the result, we can see that elements of both the matrix subtracted with their respective position in the matrix. Let’s see how to write a subtraction matrix program in java.

```// subtraction matrix program in java
class HelloWorld {
public static void main(String[] args) {

// 2D natrix
int a[][]={
{5,6,2},
{9,7,1},
{8,3,1}
};
int b[][]={
{2,4,1},
{7,6,4},
{9,2,5}
};

int n=a.length;
int m=a.length;

// matrix to store the result
int result[][]=new int[n][m];

// subtraction of the matrices
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
result[i][j]=a[i][j]-b[i][j];
}
}

System.out.println("Matrix after subtraction: ");
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
System.out.print(result[i][j]+" ");
}
System.out.println();
}
}
}```

Output:

``````Matrix after subtraction:
3 2 1
2 1 -3
-1 1 -4 ``````

In the above program, we created two matrices of the same size with random integers. After that, we have taken a matrix result to store the answer. We used two loops and subtracted each element of matrix A from the corresponding element of matrix B. In the output, we can see that the result matrix is the addition of the two matrices.

### Multiplication Matrix Program in Java:

Multiplication of the two matrices is different from addition and subtraction. To perform multiplication, we need to find the sum of the multiplication of one row and one column. For example, if we want to find the result value for the element of the first row and first column it will be the sum of the multiplication of all the elements of the row and column. Let’s understand this with an example. In the above example, we can see that if we want to find the result element then our answer is 52+67+2*9 which is equal to 70. Now, let’s see how the result element is at a particular position. Now, Let’s see the conditions for the multiplication of the two matrices. If the number of rows in one matrix is equal to the number of columns in the other matrix then only we can multiply two matrices.

Let’s see in what case multiplication is possible and in what case it is not possible. In the above example, we have matrix A of size 3×2 and matrix B of size 2×5. We can see that the number of columns in matrix A is equal to the number of rows in matrix B so multiplication is possible and the resultant matrix is of size 3×5 where 2 is the number of rows in matrix A and 5 is the number of columns in matrix B. In the above example, the number of columns in matrix A is not equal to the number of rows in matrix B thus, the multiplication of these two matrices is not possible.

Now, let’s see how to write a code for multiplication matrix program in java.

```// multiplication matrix program in java
class HelloWorld {
public static void main(String[] args) {

// 2D natrix
int a[][]={
{5,6,2},
{9,7,1},
{8,3,1}
};
int b[][]={
{2,4,1},
{7,6,4},
{9,2,5}
};

int n=a.length;
int m=a.length;

// matrix to store the result
int result[][]=new int[n][m];

// multiplication of the matrices
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
result[i][j]=0;
for(int k=0;k<n;k++){
result[i][j]+=a[i][k]*b[k][j];
}
}
}

System.out.println("Matrix after multiplication: ");
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
System.out.print(result[i][j]+" ");
}
System.out.println();
}
}
}```

Output:

``````Matrix after multiplication:
70 60 39
76 80 42
46 52 25 ``````

Other Java Programs