title: ND4J arrays & their shapes: getting data into a list
body: <p>Consider the following code, which uses the <a href="https://nd4j.org/" rel="nofollow noreferrer">ND4J library</a> to create a simpler version of <a href="https://github.com/scikit-learn/scikit-learn/blob/a24c8b46/sklearn/datasets/samples_generator.py#L639" rel="nofollow noreferrer">the "moons" test data set</a>:</p>
  
  <pre><code>val n = 100
  val n1: Int = n/2
  val n2: Int = n-n1
  val outerX = Nd4j.getExecutioner.execAndReturn(new Cos(Nd4j.linspace(0, Math.PI, n1)))
  val outerY = Nd4j.getExecutioner.execAndReturn(new Sin(Nd4j.linspace(0, Math.PI, n1)))
  val innerX = Nd4j.getExecutioner.execAndReturn(new Cos(Nd4j.linspace(0, Math.PI, n2))).mul(-1).add(1)
  val innerY = Nd4j.getExecutioner.execAndReturn(new Sin(Nd4j.linspace(0, Math.PI, n2))).mul(-1).add(1)
  val X: INDArray = Nd4j.vstack(
    Nd4j.concat(1, outerX, innerX), // 1 x n
    Nd4j.concat(1, outerY, innerY)  // 1 x n
  ) // 2 x n
  val y: INDArray = Nd4j.hstack(
    Nd4j.zeros(n1), // 1 x n1
    Nd4j.ones(n2)   // 1 x n2
  ) // 1 x n
  println(s"# y shape: ${y.shape().toList}")                        // 1x100
  println(s"# y data length: ${y.data().length()}")                 // 100
  println(s"# X shape: ${X.shape().toList}")                        // 2x100
  println(s"# X row 0 shape: ${X.getRow(0).shape().toList}")        // 1x100
  println(s"# X row 1 shape: ${X.getRow(1).shape().toList}")        // 1x100
  println(s"# X row 0 data length: ${X.getRow(0).data().length()}") // 200    &lt;- !
  println(s"# X row 1 data length: ${X.getRow(1).data().length()}") // 100
  </code></pre>
  
  <p>On the second to last line, <code>X.getRow(0).data().length()</code> is, surprisingly, 200 not 100. On inspection this is because the structure returned by <code>data()</code> contains the entire matrix, i.e. both rows, concatenated.</p>
  
  <p>How do I get just the actual first row of the X matrix into a Java (or Scala) <code>List</code>? I could take just the first 100 items of the 200-element "first row", but that doesn't seem very elegant.</p>