0% found this document useful (0 votes)
2 views3 pages

Matrix

The document contains a Java class named MatrixA that implements various matrix operations such as reading a matrix, scalar multiplication, addition, transposition, and rotation. It utilizes a Scanner for user input to create and manipulate matrices, and includes methods to print the resulting matrices. The class also features a method for rotating the matrix's ring, although it is noted that this method is not functioning correctly.

Uploaded by

panav.golyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Matrix

The document contains a Java class named MatrixA that implements various matrix operations such as reading a matrix, scalar multiplication, addition, transposition, and rotation. It utilizes a Scanner for user input to create and manipulate matrices, and includes methods to print the resulting matrices. The class also features a method for rotating the matrix's ring, although it is noted that this method is not functioning correctly.

Uploaded by

panav.golyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

File: Untitled Document 1 Page 1 of 3

package Array;
/**
* Write a description of class MatrixA here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.util.*;
public class MatrixA
{
float [] [] mat;//This instance variable stores the value of a matrix

//This function reads a matrix


void readMat()
{
Scanner sc = new Scanner(System.in);
int r = 0;
int c = 0;

System.out.print("Enter the number of rows");


r = sc.nextInt();
System.out.print("Enter the number of columns");
c = sc.nextInt();

mat = new float[r][c];

for(int i = 0; i<r;i+=1)
{
for(int n = 0; n<c; n+=1)
{
System.out.print("Enter the element of row " + (i+1) + " and columns " +
(n+1));
mat[i][n] = sc.nextInt();
}
}
}

//It takes a 2D array and a scalar value and multiplies every array in the element by
a scalar
void scalarMultiplication (float [][] mat , float s)
{
for(int i = 0; i<mat.length;i++)
{
for(int k = 0;k<mat[i].length;k++)
{
mat[i][k] = (mat[i][k])*s;
}
}
printMat(mat);
/**
* Output:
*
* 4.0 6.0
6.0 8.0
*/
}

//This function prints a matrix


void printMat(float [][] mat)
{
for(int c = 0; c < mat.length; c++)
{
for(int m = 0 ;m<mat[c].length;m++ )
{
System.out.print(mat[c][m] + "\t");
}
File: Untitled Document 1 Page 2 of 3

System.out.println();
}
/**
* Output:
*
* 4.0 6.0
6.0 8.0
*/
}

//This function adds two matrices


void scalarAddition (float [][] mat , float [][] mat2)
{
float [][] mat_final = new float[mat.length][mat[0].length];
for(int i = 0; i<mat.length;i++)
{
for(int k = 0;k<mat[i].length;k++)
{
mat_final[i][k] = mat[i][k] + mat2[i][k];
}
}
printMat(mat_final);
/**
* Output:
*
* 6.0 3.0
4.0 7.0
*/
}

//This function changes the rows to column and the column to rows
void transposeMat (float [][] mat)
{
float [][] mat_final = new float[mat[0].length][mat.length];
for(int i = 0; i<mat[0].length;i+=1)
{
for(int k = 0;k<mat.length;k++)
{
mat_final[i][k] = mat[k][i];
}
}
printMat(mat_final);
/**
* Output:
*
* 2.0 3.0
3.0 4.0
*/
}

//This function rotates the matrix by 180 degree


void rotate1(float [][] mat)
{
int l = mat[0].length-1;
float [][] mat_final = new float[mat.length][mat[0].length];
for(int i = 0; i<=l;i++)
{
for(int k = 0; k<mat.length;k++)
{
mat_final[k][l-i] = mat[i][k];
}
}
printMat(mat_final);
/**
* Output:
File: Untitled Document 1 Page 3 of 3

7.0 4.0 1.0


8.0 5.0 2.0
9.0 6.0 3.0

*/
}

//This function rotates the matrix's ring according to the input


void rotate2(float [][] mat , int r)//not working ask sir.
{
float [][] mat_final = new float[mat.length][mat[0].length];
int l = mat[0].length-1;
for(int i = 0; i<=l;i++)
{
for(int k = 0; k<mat.length;k++)
{
if(k<=r||i<=r||k>=(mat.length - r)||i>=(l-r)||(k==l)&&(i==l))
mat_final[k][l-i] = mat[i][k];
else
mat_final[i][k]=mat[i][k];
}
}
printMat(mat);
System.out.println("-----------------------------------------------------");
printMat(mat_final);
}
}

You might also like