Search code examples
javatensornd4j

How to put a vector in INDArray of ND4J?


INDArray a = Nd4j.zeros(2,2);
a.put(1,Nd4j.create(nw float[]{1.0,2.0}));

I have tried above code but it give below error

java.lang.IllegalArgumentException: Element must be a scalar

I am new to ND4J lib. Is there a way to directly put a vector?

 /**
     * Inserts the element at the specified index
     *
     * @param indices the indices to insert into
     * @param element a scalar ndarray
     * @return a scalar ndarray of the element at this index
     */
    INDArray put(int[] indices, INDArray element);

I can see above method in source code but for that I will need to create int array which will consume memory.


Solution

  • The method you are looking for is putRow, which has the desired signature:

    INDArray putRow(int row, INDArray toPut);
    

    There is also putColumn that works similarly for columns.