How to get multiple values by index in an ojAlgo-array?

108 Views Asked by At

Let's say I have an ojAlgo-array defined like this

ArrayAnyD<Double> regularArray = ArrayAnyD.PRIMITIVE64.make(10);
regularArray.loopAll((final long[] ref) -> regularArray.set(ref, ref[0]*ref[0]));

which just contains the squares of the numbers from 0 to 9, and some indexes:

long[] idxs = {1, 3, 4, 7, 9}

Now, I'd like to do something akin to

slicedArray = regularArray[idxs]

which should give me another Array containing 1.0, 9.0, 16.0, 49.0, 81.0. How would I do that with ojAlgo?

  • regularArray.get(idxs) only gives me the first value.
  • regularArray.sliceSet(idxs, 0) returns all values from the first one onwards and ignores the following indexes.

I suspect I'd need to use some form of regularArray.loop or .loopAll, but I'm not sure how to make it work

1

There are 1 best solutions below

0
On BEST ANSWER

Here's some of what you can do:

ArrayAnyD<Double> array = ArrayAnyD.PRIMITIVE64.make(3, 3, n);

MatrixView<Double> matrices = array.matrices();

// A MatrixView is Iterable,
// but you can also go directly to the matrix you want

matrices.goToMatrix(78);

// Specific rows and columns
Access2D<Double> select = matrices.select(new int[] { 1, 2 }, new int[] { 0 });

Access2D<Double> rows = matrices.rows(1, 2); // All columns

Access2D<Double> columns = matrices.columns(0); // All rows

double value10 = matrices.doubleValue(1, 0);
double value20 = matrices.doubleValue(2, 0);

matrices.goToMatrix(99); // ...

To do all of that (exactly that way) you need v51.3.0-SNAPSHOT or later.