diff --git a/Data Structures/Matrix/Matrix.java b/Data Structures/Matrix/Matrix.java index a26dfe97..6b055362 100644 --- a/Data Structures/Matrix/Matrix.java +++ b/Data Structures/Matrix/Matrix.java @@ -35,6 +35,7 @@ public class Matrix { //test operations (valid) System.out.println("2 * m2:\n" + m2.scale(2)); + System.out.println("m2 / 2:\n" + m2.divide(2)); System.out.println("m2 + m3:\n" + m2.plus(m3)); System.out.println("m2 - m3:\n" + m2.minus(m3)); System.out.println("m2 * m3: \n"+m2.multiply(m3)); @@ -118,6 +119,24 @@ public class Matrix { return new Matrix(newData); } + + /** + * Returns this matrix divided by a factor. That is, computes sA where s is a + * constant and A is a matrix (this object). + * + * @param scalar : value to divide by + * @return A new matrix scaled by the scalar value + */ + public Matrix divide(int scalar) { + + int[][] newData = new int[this.data.length][this.data[0].length]; + + for (int i = 0; i < this.getRows(); ++i) + for(int j = 0; j < this.getColumns(); ++j) + newData[i][j] = this.data[i][j] / scalar; + + return new Matrix(newData); + } /** * Adds this matrix to another matrix.