Skip to content

Commit 6959592

Browse files
author
Christian Bender
authored
Merge pull request TheAlgorithms#330 from JeonSeongBae/JeonSeongBae
Thanks for contribution
2 parents b88e978 + d82dec9 commit 6959592

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Data Structures/Matrix/Matrix.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public static void main(String[] args) {
3535

3636
//test operations (valid)
3737
System.out.println("2 * m2:\n" + m2.scale(2));
38+
System.out.println("m2 / 2:\n" + m2.divide(2));
3839
System.out.println("m2 + m3:\n" + m2.plus(m3));
3940
System.out.println("m2 - m3:\n" + m2.minus(m3));
4041
System.out.println("m2 * m3: \n"+m2.multiply(m3));
@@ -118,6 +119,24 @@ public Matrix scale(int scalar) {
118119

119120
return new Matrix(newData);
120121
}
122+
123+
/**
124+
* Returns this matrix divided by a factor. That is, computes sA where s is a
125+
* constant and A is a matrix (this object).
126+
*
127+
* @param scalar : value to divide by
128+
* @return A new matrix scaled by the scalar value
129+
*/
130+
public Matrix divide(int scalar) {
131+
132+
int[][] newData = new int[this.data.length][this.data[0].length];
133+
134+
for (int i = 0; i < this.getRows(); ++i)
135+
for(int j = 0; j < this.getColumns(); ++j)
136+
newData[i][j] = this.data[i][j] / scalar;
137+
138+
return new Matrix(newData);
139+
}
121140

122141
/**
123142
* Adds this matrix to another matrix.

0 commit comments

Comments
 (0)