Reputation: 181
I want to set values in an array based on an index and using a computation. In numpy I would write the following:
array[array < 0] = 2 * array[array < 0]
Can something like this be achieved with ND4J as well?
Edit: And what about more complicated statements / indexing such as:
array[array2 < 0] = 2 * array3[array2 < 0]
(Assuming dimensions of array1, array2 and array3 match)
Upvotes: 0
Views: 208
Reputation: 3205
Conditional assign is what you would be the closest:
import org.nd4j.linalg.indexing.BooleanIndexing;
import org.nd4j.linalg.indexing.conditions.Conditions;
INDArray array1 = Nd4j.create(new double[] {1, 2, 3, 4, 5, 6, 7});
INDArray array2 = Nd4j.create(new double[] {7, 6, 5, 4, 3, 2, 1});
INDArray comp = Nd4j.create(new double[] {1, 2, 3, 4, 3, 2, 1});
BooleanIndexing.replaceWhere(array1, array2, Conditions.greaterThan(4));
There are quite a few conditions and things you can use in there. I would suggest using the second array and applying your arithmetic you want on a whole array and letting BooleanIndexing replace what you want selecting elements from the second array you pass in.
Upvotes: 0