Matrix

Creating a Matrix

A matrix is an interface implemented by various basic data types in matrix form. For example, the constructor for BasicIntMatrix is as follows:

public BasicIntMatrix(int rows, int columns, List<int[]> list)

Parameters

  • rows: The number of rows in the matrix.
  • columns: The number of columns in the matrix.
  • list (optional): The initial values for each position in the matrix. Note that the supported Java native data types for constructing matrices can be found in the Scalar.

Example:

To declare a 2x2 BasicIntMatrix:
int[] row1 = {1, 2};
int[] row2 = {3, 4};
BasicIntMatrix matrix = new BasicIntMatrix(2, 2, Arrays.asList(row1, row2));

Usage

  1. You can use the get method to get a value at a specific position (defined by row and column) in the matrix. The return value is a scalar:

    public Scalar get(int row, int column)
  2. You can use the set<data_type> method (where <data_type> represents the data type, for example, if the data type is Int, the method is setInt) to set the value at a specific position in the matrix. For example, to set the value at row 1, column 2 to 3:

    matrix.setInt(1, 2, 3);