Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

2 - matrix

This document provides a tutorial on matrix operations in Java, including addition, subtraction, multiplication, transposition, and determinant calculation. It explains how to implement matrices using two-dimensional arrays and includes sample code for each operation. The tutorial is aimed at helping users understand and perform various matrix manipulations through user input.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

2 - matrix

This document provides a tutorial on matrix operations in Java, including addition, subtraction, multiplication, transposition, and determinant calculation. It explains how to implement matrices using two-dimensional arrays and includes sample code for each operation. The tutorial is aimed at helping users understand and perform various matrix manipulations through user input.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Matrix Programs in Java

A Matrix is a rectangular array. The elements are arranged in the rows and columns. In this
tutorial, we will look at some matrix programs in Java.

Graphical Representation of Matrix

Matrix in Java
We can implement a matrix using two dimensional array in Java. The element at row “r” and
column “c” can be accessed using index “array[r][c]”.

Matrix Programs in Java


Since we are using two-dimensional arrays to create a matrix, we can easily perform various
operations on its elements. In this tutorial, we will learn how to create a matrix from user input.
Then we will add, subtract, and multiply two matrices and print the result matrix on the console.

1. Adding Two Matrix


Here is the simple program to populate two matrices from the user input. Then add its elements
at the corresponding indices to get the addition of the matrices. Finally, we will print the sum of
the matrices.
import java.util.Scanner;

public class MatrixPrograms {

public static void main(String[] args) {


System.out.println("Please enter the rows in the matrix");
Scanner sc = new Scanner(System.in);
int row = sc.nextInt();
System.out.println("Please enter the columns in the
matrix");
int column = sc.nextInt();

1
int[][] first = new int[row][column];
int[][] second = new int[row][column];

for (int r = 0; r < row; r++) {


for (int c = 0; c < column; c++) {
System.out.println(String.format("Enter
first[%d][%d] integer", r, c));
first[r][c] = sc.nextInt();
}
}

for (int r = 0; r < row; r++) {


for (int c = 0; c < column; c++) {
System.out.println(String.format("Enter
second[%d][%d] integer", r, c));
second[r][c] = sc.nextInt();
}
}

// close the scanner


sc.close();

// print both matrices


System.out.println("First Matrix:\n");
print2dArray(first);

System.out.println("Second Matrix:\n");
print2dArray(second);

// sum of matrices
sum(first, second);
}

// below code doesn't take care of exceptions


private static void sum(int[][] first, int[][] second) {
int row = first.length;
int column = first[0].length;
int[][] sum = new int[row][column];

for (int r = 0; r < row; r++) {


for (int c = 0; c < column; c++) {
sum[r][c] = first[r][c] + second[r][c];
}
}

System.out.println("\nSum of Matrices:\n");
print2dArray(sum);
}

private static void print2dArray(int[][] matrix) {


for (int r = 0; r < matrix.length; r++) {
for (int c = 0; c < matrix[0].length; c++) {
System.out.print(matrix[r][c] + "\t");

2
}
System.out.println();
}
}
}
2. Subtracting Two Matrices
Here is the function to subtraction second matrix elements from the first matrix and then print
the result matrix.
private static void subtract(int[][] first, int[][] second) {
int row = first.length;
int column = first[0].length;
int[][] sum = new int[row][column];

for (int r = 0; r < row; r++) {


for (int c = 0; c < column; c++) {
sum[r][c] = first[r][c] - second[r][c];
}
}

System.out.println("\nSubtraction of Matrices:\n");
print2dArray(sum);
}

3. Multiplying Two Matrices


Below method will multiply the matrix elements and print the result matrix.

import java.util.Scanner;

public class MatrixMultiplication

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

System.out.println("Enter The Number Of Rows In First


Matrix");

int rowsInFirst = sc.nextInt();

System.out.println("Enter The Number Of Columns In First


Matrix / Rows In Second Matrix");

int colsInFirstRowsInSecond = sc.nextInt();

System.out.println("Enter The Number Of Columns In Second


Matrix");

int colsInSecond = sc.nextInt();

3
int[][] matrix1 = new
int[rowsInFirst][colsInFirstRowsInSecond];

int[][] matrix2 = new


int[colsInFirstRowsInSecond][colsInSecond];

int[][] product = new int[rowsInFirst][colsInSecond];

System.out.println("Enter The Data For First Matrix :");

for (int i = 0; i < rowsInFirst; i++)

for (int j = 0; j < colsInFirstRowsInSecond; j++)

matrix1[i][j] = sc.nextInt();

System.out.println("Enter The Data For Second Matrix :");

for (int i = 0; i < colsInFirstRowsInSecond; i++)

for (int j = 0; j < colsInSecond; j++)

matrix2[i][j] = sc.nextInt();

System.out.println("First Matrix = ");

for (int i = 0; i < rowsInFirst; i++)

for (int j = 0; j < colsInFirstRowsInSecond; j++)

System.out.print(matrix1[i][j]+"\t");

4
}

System.out.println();

System.out.println("Second Matrix = ");

for (int i = 0; i < colsInFirstRowsInSecond; i++)

for (int j = 0; j < colsInSecond; j++)

System.out.print(matrix2[i][j]+"\t");

System.out.println();

System.out.println("Product = ");

for (int i = 0; i < rowsInFirst; i++)

for (int j = 0; j < colsInSecond; j++)

for (int k = 0; k < colsInFirstRowsInSecond; k++)

product[i][j] += matrix1[i][k] * matrix2[k][j];

for (int i = 0; i < rowsInFirst; i++)

for (int j = 0; j < colsInSecond; j++)

5
{

System.out.print(product[i][j]+"\t");

System.out.println();

4. Transpose Of A Matrix
Transpose of a matrix is formed by turning all rows of a matrix into columns and columns into rows.

import java.util.Scanner;

public class MatrixTranspose


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

System.out.println("Enter The Number Of Rows");

int rows = sc.nextInt();

System.out.println("Enter The Number Of Columns");

int cols = sc.nextInt();

int[][] matrix = new int[rows][cols];

int[][] transpose = new int[cols][rows];

System.out.println("Enter The Data For Matrix :");

for (int i = 0; i < rows; i++)


{
for (int j = 0; j < cols; j++)
{
matrix[i][j] = sc.nextInt();
}
}

System.out.println("Your Matrix is :");

for (int i = 0; i < rows; i++)


{

6
for (int j = 0; j < cols; j++)
{
System.out.print(matrix[i][j]+" ");
}

System.out.println();
}

System.out.println("Transpose of Matrix is :");

for (int i = 0; i < rows; i++)


{
for (int j = 0; j < cols; j++)
{
transpose[j][i] = matrix[i][j];
}
}

for (int i = 0; i < cols; i++)


{
for (int j = 0; j < rows; j++)
{
System.out.print(transpose[i][j]+"\t");
}

System.out.println();
}
}
}

5. Java recursive program to compute the determinant of a


matrix
import java.io.*;

import java.lang.*;

class Matrixdet{

public static double determinent(int b[][],int m)

int c[][]=new int[m][m];

int i,j,k;

double sum=0;

if(m==1) { sum=b[0][0]; }

7
else if(m==2)

sum=(( b[0][0]*b[1][1] ) - ( b[0][1]*b[1][0] ));

else

for(int p=0;p<m;p++)

int h=0;k=0;

for(i=1;i<m;i++)

for(j=0;j<m;j++)

if(j==p)

continue;

c[h][k]=b[i][j];

k++;

if(k==m-1)

h++;

k=0;

sum=sum+b[0][p]*Math.pow((-1),p)*determinent(c,m-1);

8
}

return sum;

public static void main(String[] args) throws IOException{

DataInputStream in=new DataInputStream(System.in);

System.out.println("\nMATRIX :");

System.out.print("\nENTER THE ORDER OF THE MATRIX YOU HAVE : ");

int order=Integer.parseInt(in.readLine());

int[][] A = new int[order][order];

System.out.println("\nENTER ELEMENTS OF MATRIX : ");

for (int i=0 ; i < order ; i++)

for (int j=0 ; j < order ; j++)

A[i][j] = Integer.parseInt(in.readLine());

System.out.println("MATRIX : ");

for (int i=0 ; i < order ; i++)

{ System.out.println();

for (int j=0 ; j < order ; j++)

System.out.print(A[i][j]+" ");

9
}

System.out.println();

System.out.println("\nDETERMINENT OF MATRIX : "+ determinent(A,order));

10

You might also like