> For the complete documentation index, see [llms.txt](https://ondrej-kvasnovsky-2.gitbook.io/algorithms/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ondrej-kvasnovsky-2.gitbook.io/algorithms/data-structures/matrix.md).

# Matrix

[Matrix](https://en.wikipedia.org/wiki/Matrix_%28mathematics%29) is also called 2-dimensional array.

```
class NaiveMatrix {

    private Integer[][] values;

    public NaiveMatrix(int n, int m) {
        this.values = new Integer[n][m];
        for (int row = 0; row < values.length; row++) {
            for (int column = 0; column < values[row].length; column++) {
                values[row][column] = 0;
            }
        }
    }

    public void set(int row, int column, Integer value) {
        values[row][column] = value;
    }

    public Integer get(int i, int j) {
        return values[i][j];
    }

    public String toString() {
        StringBuilder result = new StringBuilder();
        for (Integer[] row : values) {
            for (Integer value : row) {
                result.append(value);
                result.append(" ");
            }
            result.append("\n");
        }
        return result.toString();
    }
}
```

Lets try to use this matrix implementation.

```
public class MatrixDemo {

    public static void main(String... args) {
        NaiveMatrix matrix = new NaiveMatrix(2, 3);
        matrix.set(0, 0, 5);
        matrix.set(0, 1, 5);
        matrix.set(0, 2, 5);
        System.out.println(matrix);
    }
}
```

The code above prints out the following.

```
5 5 5 
0 0 0
```

## Matrix operations

There are many operations that are described in [linear algebra](https://www.khanacademy.org/math/algebra-home/alg-matrices). We are going to implement couple of these. We are going to enhance `NaiveMatrix` class with more operations.

### Addition

Adds to matrices together, summing each element on that position.

```
    public NaiveMatrix add(NaiveMatrix matrix) {
        NaiveMatrix sum = new NaiveMatrix(values.length, values[0].length);
        for (int row = 0; row < values.length; row++) {
            for (int column = 0; column < values[row].length; column++) {
                sum.values[row][column] = values[row][column] + matrix.values[row][column];
            }
        }
        return sum;
    }
```

Then we can create two matrices and sum them together.

```
public class MatrixDemo {

    public static void main(String... args) {
        NaiveMatrix matrix = new NaiveMatrix(2, 3);
        matrix.set(0, 0, 5);
        matrix.set(0, 1, 5);
        matrix.set(0, 2, 5);
        System.out.println(matrix);

        NaiveMatrix matrix2 = new NaiveMatrix(2, 3);
        matrix2.set(0, 0, 1);
        NaiveMatrix sum = matrix.add(matrix2);
        System.out.println(sum);
    }
}
```

Here is how the output would look like.

```
5 5 5 
0 0 0 

6 5 5 
0 0 0
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ondrej-kvasnovsky-2.gitbook.io/algorithms/data-structures/matrix.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
