1

Imagine two INDArray:

  • a1.shape() = [2, 3] and a1 is full of 1;

  • a2.shape() = [1, 2] and a2 is full of 2.

I would like to perform an addition between them such as:

?> result = a1.add(0, a2)
?> print(result)
[[3, 3, 1], [1, 1, 1]]
?> result = a1.add(1, a2)
?> print(result)
[[1, 1, 1], [3, 3, 1]]
?> result = a1.add(1, 1, a2)
?> print(result)
[[1, 1, 1], [1, 3, 3]]

I have tried to first select a sub-array:

?> subarray = a1.get(NDArrayIndex.interval(0, 1), NDArrayIndex.interval(0, 2))
?> print(subarray)
[1, 1]

Perform the addition:

?> subarray = subarray.add(a2)
?> print(subarray)
[3, 3]

But I can't figure out how to insert 'subarray' into 'a1' at the good position...

Note: I simplified the problem for the sake of the explanation. The arrays being processed are 4D-arrays.

Theophile Champion
  • 453
  • 1
  • 4
  • 20

1 Answers1

1

I just found the following function in the documentation:

INDArray put(INDArrayIndex[] indices, INDArray element)

This can be used as follow to solve this question:

a1.put(
  new INDArrayIndex[] {NDArrayIndex.interval(0, 1), NDArrayIndex.interval(0, 2)},
  subarray
);

Indeed NDArrayIndex implement the INDArrayIndex interface.

Theophile Champion
  • 453
  • 1
  • 4
  • 20