Introduction
In this blog, I will explain about matrix manipulation in Java. It is very simple in Java Programming. The output will be displayed in the Run module.
Software Requirement
JDK1.3.
Matrix Manipulation
In this program, I explain about matrix addition, subtraction, and multiplication.
Simple program
- class mat
- {
- public static void main(String arg[])
- {
- int a[][] = {{1,2},{3,4}};
- int b[][] = {{1,2},{3,4}};
-
- int c[][] = new int[2][2];
-
- int i,j,k;
-
- System.out.println("\nGiven A Matrix is...");
- for(i=0;i<2;i++)
- {
- for(j=0;j<2;j++)
- {
- System.out.print(a[i][j]+"\t");
- }
- System.out.println("\n");
- }
-
- System.out.println("\nGiven B Matrix is...");
- for(i=0;i<2;i++)
- {
- for(j=0;j<2;j++)
- {
- System.out.print(b[i][j]+"\t");
- }
- System.out.println("\n");
- }
-
-
- for(i=0;i<2;i++)
- {
- for(j=0;j<2;j++)
- {
- c[i][j] = a[i][j] + b[i][j];
- }
- }
-
- System.out.println("\nMatrix Addition is...");
- for(i=0;i<2;i++)
- {
- for(j=0;j<2;j++)
- {
- System.out.print(c[i][j]+"\t");
- }
- System.out.println("\n");
- }
-
- for(i=0;i<2;i++)
- {
- for(j=0;j<2;j++)
- {
- c[i][j] = a[i][j] - b[i][j];
- }
- }
-
- System.out.println("\nMatrix Subtraction is...");
- for(i=0;i<2;i++)
- {
- for(j=0;j<2;j++)
- {
- System.out.print(c[i][j]+"\t");
- }
- System.out.println("\n");
- }
-
- for(i=0;i<2;i++)
- {
- for(j=0;j<2;j++)
- {
- for(k=0;k<2;k++)
- {
- c[i][j] = c[i][j] + a[i][k] * b[k][j];
- }
- }
- }
-
- System.out.println("\nMatrix Multiplication is...");
- for(i=0;i<2;i++)
- {
- for(j=0;j<2;j++)
- {
- System.out.print(c[i][j]+"\t");
- }
- System.out.println("\n");
- }
-
- }
- }
-
Explanation
In this blog, I will explain about matrix manipulation in Java programming. The output will be displayed in the Run module.
Output