1

In the java library ojAlgo, how can I slice a matrix or extract a sub-matrix from an existing one?

For example, matrix A = [[1,2,3],[4,5,6],[7,8,9]].

I am looking for a method which looks like slice(a,b,c,d) where a, b are the start and end indexes of the rows and c,d are the start and end indexes of the columns.

For example, if I call A.slice(1,3,1,3), it should return [[5,6],[8,9]].

I tried the slice method in SparseStore, but it does not work as I expected.

Jazzepi
  • 5,259
  • 11
  • 55
  • 81
JYY
  • 205
  • 2
  • 9

1 Answers1

2

The "slice" methods always return something 1D. You can slice out an index range, row, column, diagonal...

If you want a sub-view of a matrix you can do it this way:

matrix.logical().limits(3, 3).offsets(1, 1).get();

or with your example since the original matrix is 3x3, it's enough to do:

matrix.logical().offsets(1, 1).get();
apete
  • 1,250
  • 1
  • 10
  • 16
  • With recent versions of ojAlgo you can skip 'logical()' and 'get()'. In the 3x3 case you simply do: matrix.offsets(1, 1); – apete Oct 07 '22 at 10:01