# Multivariable Linear Regression….In Java Part 1

## Matrices: The first step

`public class Matrix {     private double[][] matrix;      private int height;     private int width;      public Matrix(double[][] values){          this.height = values.length;           this.width = values[0].length;          this.matrix = values     }`
`public Matrix scalarMultiplication(double scalar) {    double[][] arr = new double[this.getHeight()][this.getWidth()];    for (int i = 0; i < arr.length; i++) {        for (int j = 0; j < arr[i].length; j++) {            arr[i][j] = (double)(this.getElement(i, j) * scalar);        }    }    Matrix ans = new Matrix(arr);    return ans;}public Matrix scalarDivision(double scalar) {    double[][] arr = new double[this.getHeight()][this.getWidth()];    for (int i = 0; i < arr.length; i++) {        for (int j = 0; j < arr[i].length; j++) {            arr[i][j] = (double) (this.getElement(i, j) / scalar);        }    }    Matrix ans = new Matrix(arr);    return ans;}public Matrix addMatrix(Matrix b) {    if (b.getWidth() != this.getWidth() || b.getHeight() != this.getHeight()) {        System.out.println("Matrices are not the same dimensions");        return null;    }    double[][] arr = new double[this.getHeight()][this.getWidth()];    for (int i = 0; i < arr.length; i++) {        for (int j = 0; j < arr[i].length; j++) {            arr[i][j] = this.getElement(i, j) + b.getElement(i, j);        }    }    Matrix ans = new Matrix(arr);    return ans;}public Matrix subtractMatrix(Matrix b) {    if (b.getWidth() != this.getWidth() || b.getHeight() != this.getHeight()) {        System.out.println("Matrices are not the same dimensions");        return null;    }    double[][] arr = new double[this.getHeight()][this.getWidth()];    for (int i = 0; i < arr.length; i++) {        for (int j = 0; j < arr[i].length; j++) {            arr[i][j] = this.getElement(i, j) - b.getElement(i, j);        }    }    Matrix ans = new Matrix(arr);    return ans;}`
`public Matrix multiplyMatrix(Matrix b) {    double[][] arr = new double[this.getHeight()][b.getWidth()];    for (int row = 0; row < this.getHeight(); row++) {        for (int col = 0; col < b.getWidth(); col++) {            //multiply rowth row with colth column            arr[row][col] = multiplyCells(this, b, row, col);        }    }    Matrix ans = new Matrix(arr);    return ans;}public double multiplyCells(Matrix a, Matrix b, int row, int col) {    double val = 0;    for (int i = 0; i < a.getWidth(); i++) {        val += a.getElement(row, i) * b.getElement(i, col);    }    return val;}`
`public Matrix transpose() {    double[][] arr = new double[this.getWidth()][this.getHeight()];    for (int row = 0; row < this.getHeight(); row++) {        for (int col = 0; col < this.getWidth(); col++) {            arr[col][row] = this.getElement(row, col);        }    }    Matrix ans = new Matrix(arr);    return ans;}`

--

--

--

## More from Arnav Kartikeya

A high school student interested in cognitive science and programming

Love podcasts or audiobooks? Learn on the go with our new app.

## Arnav Kartikeya

A high school student interested in cognitive science and programming