forked from iamAnki/Codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
81 lines (77 loc) · 1.7 KB
/
Matrix.java
File metadata and controls
81 lines (77 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.io.*;
import java.util.*;
class Matrix {
int row;
int col;
int[][] a;
public Matrix(int r, int c)
{
row = r;
col = c;
a = new int[row][col];
}
public int getrow()
{
return row;
}
public int getcol()
{
return col;
}
public int getelement(int r, int c)
{
return a[r][c];
}
public void setelement(int r, int c, int value)
{
a[r][c] = value;
}
public static Matrix add(Matrix a, Matrix b)
{
if((a.row != b.row) || (a.col != b.col))
{
System.out.println("Matrices can not be added");
return new Matrix(0,0);
}
else
{
Matrix m = new Matrix(a.row,a.col);
for(int i = 0;i<m.row;i++)
{
for(int j = 0;j<m.col;j++)
{
m.setelement(i,j,(a.getelement(i,j)+b.getelement(i,j)));
}
}
return m;
}
}
public static Matrix mult(Matrix a, Matrix b)
{
Matrix m = new Matrix(a.row,b.col);
for(int i = 0;i<a.row;i++)
{
for(int j = 0;j<b.col;j++)
{
int s = 0;
for(int k = 0;k<a.col;k++)
{
s = s+(a.getelement(i,k)*b.getelement(k,j));
}
m.setelement(i,j,s);
}
}
return m;
}
public void printmat()
{
for(int i = 0;i<row;i++)
{
for(int j = 0;j<col;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println("");
}
}
}